1 16 17 package org.apache.myfaces.examples.listexample; 18 19 import java.util.*; 20 21 public class PagedSortableCarList extends SortableList 22 { 23 private List cars = new ArrayList(); 24 25 public PagedSortableCarList() 26 { 27 super("type"); 28 for (int i = 100; i < 900; i++) 29 { 30 cars.add(new SimpleCar(i, "Car Type " + i, (i%2==0) ? "blue" : "green")); 31 } 32 } 33 34 public List getCars() 35 { 36 sort(getSort(), isAscending()); 37 return cars; 38 } 39 40 protected boolean isDefaultAscending(String sortColumn) 41 { 42 return true; 43 } 44 45 protected void sort(final String column, final boolean ascending) 46 { 47 Comparator comparator = new Comparator() 48 { 49 public int compare(Object o1, Object o2) 50 { 51 SimpleCar c1 = (SimpleCar)o1; 52 SimpleCar c2 = (SimpleCar)o2; 53 if (column == null) 54 { 55 return 0; 56 } 57 if (column.equals("type")) 58 { 59 return ascending ? c1.getType().compareTo(c2.getType()) : c2.getType().compareTo(c1.getType()); 60 } 61 else if (column.equals("color")) 62 { 63 return ascending ? c1.getColor().compareTo(c2.getColor()) : c2.getColor().compareTo(c1.getColor()); 64 } 65 else return 0; 66 } 67 }; 68 Collections.sort(cars, comparator); 69 } 70 71 } 72 | Popular Tags |