1 Reply Latest reply on Mar 9, 2011 7:02 AM by wdfink

    Customize Sortling

    akhtar24

      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.

      1. import java.util.ArrayList; 
      2. import java.util.Comparator; 
      3. import java.util.List; 
      4.  
      5.  
      6. public class CarSort implements Comparable<CarSort>{ 
      7.      
      8.     String name; 
      9.     String color; 
      10.      
      11.     public CarSort(String name, String color){ 
      12.         this.name = name; 
      13.         this.color = color; 
      14.     }  
      15.      
      16.     public String getName() { 
      17.         return name; 
      18.     } 
      19.     public void setName(String name) { 
      20.         this.name = name; 
      21.     } 
      22.     public String getColor() { 
      23.         return color; 
      24.     } 
      25.     public void setColor(String color) { 
      26.         this.color = color; 
      27.     } 
      28.      
      29.      /*
      30.      **  Implement the natural order for this class
      31.      */ 
      32.      public int compareTo(CarSort c) 
      33.      { 
      34.          return getName().compareTo(c.getName()); 
      35.      } 
      36.       
      37.       
      38.      static class ColorComparator implements Comparator<CarSort> 
      39.      { 
      40.                   
      41.           
      42.          public int compare(CarSort c1, CarSort c2) 
      43.          { 
      44.                  String a1 = c1.getColor(); 
      45.                  String a2 = c2.getColor(); 
      46.  
      47.                  return a1.compareTo(a2); 
      48.                   
      49.                   
      50.          } 
      51.      } 
      52.       
      53.      public static void main(String[] args) 
      54.      { 
      55.          List<CarSort> carList = new ArrayList<CarSort>(); 
      56.          List<String> sortOrder = new ArrayList<String>(); 
      57.           
      58.          carList.add(new CarSort("Ford Figo","Silver")); 
      59.          carList.add(new CarSort("Santro","Blue")); 
      60.          carList.add(new CarSort("Honda Jazz","Magenta")); 
      61.          carList.add(new CarSort("Indigo V2","Red")); 
      62.            
      63.          sortOrder.add("Red"); 
      64.          sortOrder.add("Magenta"); 
      65.          sortOrder.add("Blue"); 
      66.          sortOrder.add("Silver"); 
      67.           
      68.           
      69.          // Now here I am confuse how to implement my custom sort 
      70.           
      71.           
      72.      } 
      73.     
      74.      


      Thank you for your time,

      Akhtar

        • 1. Customize Sortling
          wdfink

          This is a question for a JAVA forum, it is nothing with JBoss or JEE, nevertheless here the answer

           

          // Now here I am confuse how to implement my custom sort

          java.util.Collections.sort(carList,new ColorComparator(sortOrder));

           

          within the comparator compare you must check the order of colors, ATM you sort alphabetic by color