随笔-20  评论-3  文章-9  trackbacks-0
  1. List里面装的对象是自定义的,重载这个对象的compareTo()方法,在这个方法中根据属性A来判断大小。  
      之后用Collections.sort()对你的list进行排序
  2. 容器内元素实现Comparetor接口,Collections.sort()把它传进去就可以了
  3. 将内部对象继承comparable接口,实现一个compareTo接口,调用java.util.Collections.sort()方法
  4. Collections.sort(fileList,   new   FileNodeComparator(form.getSortName(),form.isAsc()));  
       
       
       
      class   FileNodeComparator   implements   Comparator{  
      private   String   sortName   =   "name";  
      private   boolean   isAsc   =   true;  
      FileNodeComparator(String   sortName,boolean   isAsc){  
      this.sortName   =   sortName;  
      this.isAsc   =   isAsc;  
      }  
      public   int   compare(Object   arg0,   Object   arg1)   {  
      。。。。。。。。  
      。。。。。。。。  
      。。。。。。。。  
      。。。。。。。。  
      }  
      }
  5. public class SortList {

        public static void main(String args[]) {
            String words[] = { "man", "Man", "Woman", "woman", "Manana", "manana", "ma?ana", "Ma?ana", "Mantra", "mantra",
                    "mantel", "Mantel" };

            PrintStream out = System.out;

            List list = new ArrayList(Arrays.asList(words));
            out.println("传入的list:");
            out.println(list);
            out.println();

            Collections.sort(list);
            out.println("默认的sorting:");
            out.println(list);
            out.println();

            list = new ArrayList(Arrays.asList(words));
            Comparator comp = Collections.reverseOrder();
            Collections.sort(list, comp);
            out.println("反向sorting:");
            out.println(list);
            out.println();

            list = new ArrayList(Arrays.asList(words));
            comp = new CollatorComparator();
            Collections.sort(list, comp);
            out.println("自定义排序sorting:");
            out.println(list);
            out.println();

            list = new ArrayList(Arrays.asList(words));
            comp = new CaseInsensitiveComparator();
            Collections.sort(list, comp);
            out.println("按照语种sorting:");
            out.println(list);
            out.println();

        }

        /**
         #############################################################################
         #
         # @DESCRIBE  自定义的排序规则
         # @author    Administrator
         #                                                              
         #############################################################################
         */
        public static class CaseInsensitiveComparator implements Comparator {
            public int compare(Object element1, Object element2) {
                String lower1 = element1.toString().toLowerCase();
                String lower2 = element2.toString().toLowerCase();
                return lower1.compareTo(lower2);
            }
        }
        /**
         #############################################################################
         #
         # @DESCRIBE  按照语种进行排序的规则
         # @author    Administrator
         #                                                              
         #############################################################################
         */
        public static class CollatorComparator implements Comparator {
            Collator collator = Collator.getInstance();

            public int compare(Object element1, Object element2) {
                CollationKey key1 = collator.getCollationKey(element1.toString());
                CollationKey key2 = collator.getCollationKey(element2.toString());
                return key1.compareTo(key2);
            }
        }
    }
posted on 2008-05-30 16:47 蓝山 阅读(934) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: