How to use Collections.sort () in java? [on hold]


I have the simple class - Person.



}



I cread the list of person range from 10 to 20 years old.
Now, I would like to sort the list using Collections.sort() method, but I don't understand how this works.



Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.



Your Comparator is wrong. Try this:



or



Think about the contract for Comparator. It returns an int, not boolean.



Generally in your @Override implementation of the compare() function, you will want something that returns -1, 0, or 1 (generally an int). So, in this case you would want something like this:



This will order Person a1 and Person a2 based on age. Depending on the order you want (ascending vs descending) you can do the comparison as a2.GetAge().compareTo(a1.GetAge()); and that will work just fine as well.



You will also need to consider the case that both values are equal (which is when you would return 0).

Popular posts from this blog

The Dalles, Oregon

眉山市

清晰法令