Reputation: 937
I am using java 8. I have a list of car objects . I want to sort them in a specific order.
each car objects belong to a model . i like to sort the list of car objects by model. The sorted list should be in the following order , cars with models SEDAN, then followed by BMW and then UNICORN.
import java.util.ArrayList;
import java.util.List;
enum Model {
BMW, SEDAN, UNICORN
}
public class Car {
private Model model;
private int id;
public Car(Model model, int id) {
super();
this.model = model;
this.id = id;
}
public Model getModel() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public static void main(String[] args) {
List<Car> l = new ArrayList<Car>();
Car c1 = new Car(Model.BMW, 1);
Car c2 = new Car(Model.BMW, 2);
Car c3 = new Car(Model.SEDAN, 3);
Car c4 = new Car(Model.SEDAN, 4);
Car c5 = new Car(Model.SEDAN, 5);
Car c6 = new Car(Model.UNICORN, 6);
Car c7 = new Car(Model.BMW, 7);
Car c8 = new Car(Model.BMW, 8);
l.add(c1);
l.add(c2);
l.add(c3);
l.add(c4);
l.add(c5);
l.add(c6);
l.add(c7);
l.add(c8);
}
}
so when i print the sorted list it should be like this
[Car [model=SEDAN, id=3], Car [model=SEDAN, id=4] ,Car [model=SEDAN, id=5], [model=BMW, id=1], Car [model=BMW, id=2], Car [model=BMW, id=7], Car [model=BMW, id=8], Car [model=UNICORN, id=6]]
How can i achieve this order of sorting on the model enum with a custom comparator.
appreciate if you can help
Upvotes: 3
Views: 1403
Reputation: 7279
First approach
Reorder the enum entries so the resulting order follows your requirements:
enum Model {
SEDAN, BMW, UNICORN
}
then you can sort your list as follows:
l.sort(Comparator.comparing(Car::getModel));
Note that enum values are compared by their order in code (i.e. ordinal()
)
Second approach
Assign a value to each enum entry:
enum Model {
SEDAN(0), BMW(1), UNICORN(2);
final int val;
Model(int val) { this.val = val; }
}
then you can sort your list as follows:
l.sort(Comparator.comparing((Car car) -> car.getModel().val));
Upvotes: 4
Reputation: 1086
You can assign int values to enums and give them values according to the preferences you want.
Then implement Comparable
interface for your Car
object to sort the using these assigned int
values for each model
Code
import java.util.ArrayList;
import java.util.List;
// assign enum values in ascending order here
enum Model {
SEDAN(0),
BMW(1),
UNICORN(2);
private int value;
private Model(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
// implement comparable class
public class Car implements Comparable<Car> {
private Model model;
private int id;
public Car(Model model, int id) {
super();
this.model = model;
this.id = id;
}
public Model getModel() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// compare Cars by Model int value
@Override
public int compareTo(Car otherCar) {
if (this.getModel().getValue() > otherCar.getModel().getValue())
return 1;
else return -1;
}
@Override
public String toString() {
return "Car{" +
"model=" + model +
", id=" + id +
'}';
}
}
public static List<Car> l = new ArrayList<Car>();
Car c1 = new Car(Model.BMW, 1);
Car c2 = new Car(Model.BMW, 2);
Car c3 = new Car(Model.SEDAN, 3);
Car c4 = new Car(Model.SEDAN, 4);
Car c5 = new Car(Model.SEDAN, 5);
Car c6 = new Car(Model.UNICORN, 6);
Car c7 = new Car(Model.BMW, 7);
Car c8 = new Car(Model.BMW, 8);
l.add(c1);
l.add(c2);
l.add(c3);
l.add(c4);
l.add(c5);
l.add(c6);
l.add(c7);
l.add(c8);
l.sort(Car::compareTo);
l;
Output
[Car{model=SEDAN, id=5},
Car{model=SEDAN, id=4},
Car{model=SEDAN, id=3},
Car{model=BMW, id=8},
Car{model=BMW, id=7},
Car{model=BMW, id=2},
Car{model=BMW, id=1},
Car{model=UNICORN, id=6}]
Upvotes: 2