Solved

Sorting list of objects based on child object members

Posted on 2007-11-15
5
388 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
  • 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

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

770 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