Solved

Best and effecient way to sort the List

Posted on 2007-03-23
13
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Crystal Reports Licensing Questions 4 86
convert Absolute path to relative path of FTP location 7 50
Session in java desktop 5 37
Setup Eclipse for Andriod development 2 19
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

739 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