Best and effecient way to sort the List

I have the following List which consists of Person object. The List is not sorted.  This is the example

List [person1, person3, person5, person2, person0, person4]
Person object with name field, and age field so that person1["John", 24], person3["Smith", 25], person5["Smith", 32], person2["Smith", 12], person0["Robert", 30], person4["John", 20]. I would like to sort the whole List by the name. But if the person has the same name, I want to put all of the same name's person objects together, then sort it by age. Based on the example, the sorted List would be like the following

List [person4, person1, person0, person2, person3, person5]. I need to find the best and effecient way to sort it (least cost) . The code example should be generic way, not limited to this example. Thanks
cplusplus030999Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Use a further Map<String, List<Person>>
0
 
ManishLeadCommented:
0
 
ManishLeadCommented:
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ManishLeadCommented:
0
 
CEHJCommented:
>>The code example should be generic way, not limited to this example.

That's not possibly, since the sort must be based on the specific fields of a specific object
0
 
cplusplus030999Author Commented:
Thanks for all of the responses. Actually I have implemented Comparator interface. When I say the generic way, I mean that the codes should not focus on the concrete example but are based on the specific fields of a specific object.  NOW the problem is that I am trying to look the efficient way to group the person object which has same name together. Seems that I need to sort twice: first sort by name, then group the person object together, then sort by age. Somehow I don't think that it is the best way to do that
0
 
CEHJCommented:
>>I am trying to look the efficient way to group the person object which has same name together.

That will happen anyway when you sort
0
 
cplusplus030999Author Commented:
CEHJ,

I would like the best and efecient way. Would you give the example?
0
 
CEHJCommented:
The essence of the Comparator should be

int result = (this.lastName + this.firstName).compareTo(other.lastName + other.firstName);
if (result == 0) {
      return this.age - other.age;
}
else {
      return result;
}      
0
 
cplusplus030999Author Commented:
CEHJ,

The solutions is good enough to sort by name first, then by age based on the same name. The only problem is that it is hard to group the same name's person together. For instance, I might have new object P[name, List (age)] . Based on the example, P1["John", List(20, 24)], P2["Robert", 30], P3["Smith", List(12, 25,32)] so that I can print out Like:

John     20
             24

Robert  30

Smith    12
             25
             32
For same name, It only prints one time.

CEHJ, thank you for the answer!
0
 
CEHJCommented:
:-) No problem

You could do what you want above easily enough when you print out
0
 
cplusplus030999Author Commented:
CEHJ,

It has done the sorting but not "But if the person has the same name, I want to put all of the same name's person objects together".  Thanks.
0
 
CEHJCommented:
:-)
0
All Courses

From novice to tech pro — start learning today.