sorting - How to use Comparator in Java to sort -


How do I use comparisons, but I'm having difficulty with the comparator I am getting an error in my code: / P>

  Exception in thread "main" java.lang.ClassCastException: New. People can not be put on java.lang. Similar on Java.util.Arrays. Unknown source (unknown source) at java.util.Collections.sort on Java.util.Arrays.sort (unknown source) at New.TestPeople.Main (TestPeople.java18)  

Here's my The code is:

  import java.util.Comparator; People class people Comparator {Private int id; Private string information; Private double price; Public People (Int Nude, String NewInfo, Double News) {Setid (Nude); Setinfo (newinfo); Setprice (newprice); } Public int getid () {return ID; } Public Zero Setid (Ent ID) {this.id = id; } Public string getinfo () {return information; } Public Zero setinfo (string information) {this.info = info; } Public double getprice () {return value; } Public Zero Setpres (double price) {this.price = price; } Comparison of public int (object obj1, object obj2) {integer p1 = ((people) obj1) .getid (); Integer p2 = (obj2 (people)) .getid (); If (P1> P2) {return 1; } And if (P1 & Lt; P2) {return -1; } And {return 0; }}}  
  import java.util.ArrayList; Import java.util.Collections; Public Class Test People {Public Static Zero Main (String [] Args) {ArrayList peps = new ArrayList (); Peps.add (new people (123, "M", 14.25)); Peps.add (New People (234, "M", 6.21)); Peps.add (new people (362, "F", 9.23)); Peps.add (new people (111, "M", 65.99)); Peps.add (new people (535, "F", 9.23)); Collections.sort (peps); For (int i = 0; i & lt; peps.size (); i ++) {System.out.println (peps.get (i)); }}}  

I believe that he has to do something with casting compared to the method, but I was playing with it and still could not get the solution

There are some weird things with your example class:

  • Price < / Code> and information (nothing else for the object, not the people);
  • When a class is to name something as plural, it suggests that it is an abstract of more than one thing

Lt; Person & gt; People = Areas.Slist (New Person ("Joe", 24), New Person ("Pete", 18), New Person ("Chris", 21)); collection. Source (People, New Lexicographic Comparator ()); Println (people); collection. Short (People, New Age Comparator ()); Println (people); }} Comparison with class Lexicographic Comparator & lt; Person & gt; {@ Comparison of Override Public Ent (person A, person B) {Return to a.name.compareToIgnoreCase (b.name)); }} Class AgeComparator implements Comparator & lt; Person & gt; {@ Compared to Override Public Int (person A, person B) {Back AAG & Lt; BAG? -1: a.age == B.AG? 0: 1; }} Class person {string name; Old age; Person (string n, int a) {name = n; Age = a; } @ Override public string toastring () {return string.format ("{name =% s, age =% d}", name, age); }}

edit

and the same Java 8 demo will look like this:

  public class ComparatorDemo {public static zero major (String [] args) {List & lt; Person & gt; People = Areas.Slist (New Person ("Joe", 24), New Person ("Pete", 18), New Person ("Chris", 21)); collection. Short (people, (A, B) - & gt; ANNA.Power to Ignor Sees (BNN)); Println (people); collection. Short (people, (A, B) -> AAG and LT; BJ? -1: A.A.G. = B.E.G. 0: 1); Println (people); }}  

Comments