1 5 package org.exoplatform.services.communication.sms.util; 6 7 import java.util.ArrayList ; 8 import java.util.Collections ; 9 import java.util.Comparator ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 13 18 public class Sort { 19 20 public static final int ASC = 0; 21 public static final int DESC = 1; 22 23 public Sort() { 24 } 25 26 public static List sort(List list, String fieldName, int order) { 27 28 Collections.sort(list, new SortComparator(fieldName)); 29 if (order == Sort.DESC) Collections.reverse(list); 30 31 return list; 32 } 33 34 public static void main(String [] args) { 35 ArrayList list = new ArrayList (); 36 list.add(new TestBean("ove", "toyen")); 37 list.add(new TestBean("tuva", "lokka")); 38 list.add(new TestBean("knut", "st.haugen")); 39 list.add(new TestBean("tor-egil", "Ljan")); 40 List sortedList = Sort.sort(list, "name", Sort.DESC); 41 for (Iterator iterator = sortedList.iterator(); iterator.hasNext();) { 42 TestBean myBean = (TestBean) iterator.next(); 43 System.out.println(myBean.getName()); 44 } 45 } 46 47 static public void putInSorted(List a, Comparator c) { 48 int cnt = 0; 49 for (Iterator iterator = a.iterator(); iterator.hasNext();) { 50 Object temp = (Object ) iterator.next(); 51 52 int j = cnt++ - 1; 53 for (; j >= 0 && c.compare(temp, a.get(j)) < 0; j--) 54 a.set(j + 1, a.get(j)); 55 56 a.set(j + 1, temp); 57 58 } 59 } 60 61 } 62 63 | Popular Tags |