Solved

Sorting list of objects based on child object members

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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:
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:

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now