Hello,
I want to develop one sorting demo for car list. i am using data table to display car list (JSF datatable). Now actually i want to sort the list by car color. here it is not sort by alphabetic order. I want to use my custom order list like Red car come first, then Blue, etc.
For that i try to use Java Comparator and comparable but its allow to sort in alphabetic order only.
So, can any one guide me the way to implement and technique to use that wise the sorting became faster.
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
-
-
- public class CarSort implements Comparable<CarSort>{
-
- String name;
- String color;
-
- public CarSort(String name, String color){
- this.name = name;
- this.color = color;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
-
-
- public int compareTo(CarSort c)
- {
- return getName().compareTo(c.getName());
- }
-
-
- static class ColorComparator implements Comparator<CarSort>
- {
-
-
- public int compare(CarSort c1, CarSort c2)
- {
- String a1 = c1.getColor();
- String a2 = c2.getColor();
-
- return a1.compareTo(a2);
-
-
- }
- }
-
- public static void main(String[] args)
- {
- List<CarSort> carList = new ArrayList<CarSort>();
- List<String> sortOrder = new ArrayList<String>();
-
- carList.add(new CarSort("Ford Figo","Silver"));
- carList.add(new CarSort("Santro","Blue"));
- carList.add(new CarSort("Honda Jazz","Magenta"));
- carList.add(new CarSort("Indigo V2","Red"));
-
- sortOrder.add("Red");
- sortOrder.add("Magenta");
- sortOrder.add("Blue");
- sortOrder.add("Silver");
-
-
-
-
-
- }
-
-
- }
Thank you for your time,
Akhtar