比較字串,排序,比較字串排序

來源:互聯網
上載者:User

比較字串,排序,比較字串排序
   List<Map<String, Object>> queryData = configDao.queryConfig(expressions, conditions, expressions);  Collections.sort(queryData, new Comparator<Map<String, Object>>() {public int compare(Map<String, Object> o1, Map<String, Object> o2) {// Integer name1 = Integer.valueOf(o1.get("category").toString()) ;//name1是從你list裡面拿出來的一個// Integer name2 = Integer.valueOf(o2.get("category").toString()) ; //name1是從你list裡面拿出來的第二個name// return name1.compareTo(name2);return o1.get("category").toString().compareTo(o2.get("category").toString());}}); Comparator內建函數,queryDate是需要排列的list數組,後面是定序,  compare(a,b)方法:根據第一個參數小於、等於或大於第二個參數分別返回負整數、零或正整數。
equals(obj)方法:僅當指定的對象也是一個 Comparator,並且強行實施與此 Comparator 相同的排序時才返回 true。
 Collections.sort(list, new PriceComparator());的第二個參數返回一個int型的值,就相當於一個標誌,告訴sort方法按什麼順序來對list進行排序。 例子: package com.tjcyjd.comparator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.GregorianCalendar; import java.util.Iterator; import java.util.List;   public class UseComparator {   public static void main(String args[]) {   List<Book> list = new ArrayList<Book>(); // 數組序列   Book b1 = new Book(10000, "紅樓夢", 150.86, new GregorianCalendar(2009, 01, 25), "曹雪芹、高鄂");   Book b2 = new Book(10001, "三國演義", 99.68, new GregorianCalendar(2008, 7, 8), "羅貫中 ");   Book b3 = new Book(10002, "水滸傳", 100.8, new GregorianCalendar(2009, 6, 28), "施耐庵 ");   Book b4 = new Book(10003, "西遊記", 120.8, new GregorianCalendar(2011, 6, 8), "吳承恩");   Book b5 = new Book(10004, "天龍八部", 10.4, new GregorianCalendar(2011, 9, 23), "搜狐");   list.add(b1);   list.add(b2);   list.add(b3);   list.add(b4);   list.add(b5);   // Collections.sort(list); //沒有預設比較子,不能排序   System.out.println("數組序列中的元素:");   myprint(list);     Collections.sort(list, new PriceComparator()); // 根據價格排序   System.out.println("按書的價格排序:");   myprint(list);    Collections.sort(list, new CalendarComparator()); // 根據時間排序   System.out.println("按書的出版時間排序:");   myprint(list); }    // 自訂比較子:按書的價格排序     static class PriceComparator implements Comparator {     public int compare(Object object1, Object object2) {// 實現介面中的方法     Book p1 = (Book) object1; // 強制轉換     Book p2 = (Book) object2;     return new Double(p1.price).compareTo(new Double(p2.price));      }    }    // 自訂比較子:按書出版時間來排序   static class CalendarComparator implements Comparator {     public int compare(Object object1, Object object2) {// 實現介面中的方法     Book p1 = (Book) object1; // 強制轉換     Book p2 = (Book) object2; return p2.calendar.compareTo(p1.calendar);     }  }    // 自訂方法:分行列印輸出list中的元素   public static void myprint(List<Book> list) {     Iterator it = list.iterator(); // 得到迭代器,用於遍曆list中的所有元素     while (it.hasNext()) {// 如果迭代器中有元素,則返回true       System.out.println("\t" + it.next());// 顯示該元素           } 
    } 
}    重寫compare方法還能加列數  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.