Solved

Sorting list of objects based on child object members

Posted on 2007-11-15
5
394 Views
Last Modified: 2013-12-29
Here is a simple version of my problem, consider the following 2 classes. Given a list of Car objects, I would like to sort first by Category name, then by Car name. Is there anything in the api that can help with this task? Or what sort algorithm would be best consider the Lists I'm working with would not exceed 1000 entries. Thank you.
public Car {
  public String name;
  public Category cat;
  .....
}
 
public Category {
  public String name;
  ....
}

Open in new window

0
Comment
Question by:saic_gco
[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
  • 2
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20292735
// by name

Collection.sort(cars, new Comparator()
{
   public int compare(Object a, Object b)
   {
       Car car1 = (Car) a;
       Car car2 = (Car) b;
       return car1.getName().compareTo(car2.getName());
   }
});
System.out.println(cars);

// by category

Collection.sort(cars, new Comparator()
{
   public int compare(Object a, Object b)
   {
       Car car1 = (Car) a;
       Car car2 = (Car) b;
       return car1.getCatgory().getName().compareTo(car2.getCategory().getName());
   }
});
System.out.println(cars);
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20292743
or to sort by cat, then by name (if cat same) use:

Collection.sort(cars, new Comparator()
{
   public int compare(Object a, Object b)
   {
       Car car1 = (Car) a;
       Car car2 = (Car) b;
       int bycat = car1.getCatgory().getName().compareTo(car2.getCategory().getName());
       return bycat==0 ? car1.getName().compareTo(car2.getName()) : bycat;
   }
});
System.out.println(cars);
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20292848
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20293126
hey objects,
it not Collection.sort
it is    Collections.sort  :)))
 
0
 
LVL 2

Author Closing Comment

by:saic_gco
ID: 31409438
objects.complimentStringVector.add("you da man");
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add projects t working set in maven 2 70
Java class and jar 3 61
listing all the respondents to a twitter feed - Java 5 47
JVM error from eclipse 1 26
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

738 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