Solved

Best and effecient way to sort the List

Posted on 2007-03-23
13
186 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:cplusplus030999
  • 6
  • 4
  • 3
13 Comments
 
LVL 11

Expert Comment

by:Manish
ID: 18784552
0
 
LVL 11

Expert Comment

by:Manish
ID: 18784557
0
 
LVL 11

Expert Comment

by:Manish
ID: 18784560
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18784661
>>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
 

Author Comment

by:cplusplus030999
ID: 18785239
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 18785254
>>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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:cplusplus030999
ID: 18785341
CEHJ,

I would like the best and efecient way. Would you give the example?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18785366
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
 

Author Comment

by:cplusplus030999
ID: 18786323
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 18786359
:-) No problem

You could do what you want above easily enough when you print out
0
 

Author Comment

by:cplusplus030999
ID: 18790560
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 18791126
Use a further Map<String, List<Person>>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18798300
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 51
java set up 1 46
wordappend challenge 8 85
Java Timer (static) 9 15
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now