Solved

Best and effecient way to sort the List

Posted on 2007-03-23
13
188 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

777 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