Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Best and effecient way to sort the List

Posted on 2007-03-23
13
Medium Priority
?
192 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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 500 total points
ID: 18791126
Use a further Map<String, List<Person>>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18798300
:-)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

721 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