Solved

Sorting list of objects based on child object members

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 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

19 Experts available now in Live!

Get 1:1 Help Now