1 16 package org.apache.myfaces.examples.listexample; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.Comparator ; 21 import java.util.List ; 22 23 28 public class SimpleSortableCarList 29 extends SortableList 30 { 31 private List _cars; 32 33 public SimpleSortableCarList() 34 { 35 super("type"); 36 37 _cars = new ArrayList (); 38 _cars.add(new SimpleCar(1, "car A", "red")); 39 _cars.add(new SimpleCar(1, "car B", "blue")); 40 _cars.add(new SimpleCar(1, "car C", "green")); 41 _cars.add(new SimpleCar(1, "car D", "yellow")); 42 _cars.add(new SimpleCar(1, "car E", "orange")); 43 } 44 45 public List getCars() 46 { 47 sort(getSort(), isAscending()); 48 return _cars; 49 } 50 51 protected boolean isDefaultAscending(String sortColumn) 52 { 53 return true; 54 } 55 56 protected void sort(final String column, final boolean ascending) 57 { 58 Comparator comparator = new Comparator () 59 { 60 public int compare(Object o1, Object o2) 61 { 62 SimpleCar c1 = (SimpleCar)o1; 63 SimpleCar c2 = (SimpleCar)o2; 64 if (column == null) 65 { 66 return 0; 67 } 68 if (column.equals("type")) 69 { 70 return ascending ? c1.getType().compareTo(c2.getType()) : c2.getType().compareTo(c1.getType()); 71 } 72 else if (column.equals("color")) 73 { 74 return ascending ? c1.getColor().compareTo(c2.getColor()) : c2.getColor().compareTo(c1.getColor()); 75 } 76 else return 0; 77 } 78 }; 79 Collections.sort(_cars, comparator); 80 } 81 } 82 | Popular Tags |