Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Sorting list of objects based on child object members

Posted on 2007-11-15
Medium Priority
417 Views
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;
....
}
``````
0
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
• 2
• 2

LVL 92

Expert Comment

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

objects earned 2000 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

ID: 20292848
0

LVL 9

Expert Comment

ID: 20293126
hey objects,
it not Collection.sort
it is    Collections.sort  :)))

0

LVL 2

Author Closing Comment

ID: 31409438
0

## Featured Post

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
###### Suggested Courses
Course of the Month5 days, 20 hours left to enroll