Solved

Sorting list of objects based on child object members

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

830 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