Compare string, sort, and compare string sorting

Source: Internet
Author: User

Compare string, sort, and compare string sorting
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 is an Integer obtained from your list. // Integer name2 = Integer. valueOf (o2.get ("category "). toString (); // name1 is the second name from your list // return name1.compareTo (name2); return o1.get ("category "). toString (). compareTo (o2.get ("category "). toString () ;}}); Comparator built-in function. queryDate is the list array to be sorted, followed by the sorting rule,Compare (a, B) method: return a negative integer, zero, or positive integer based on the first parameter smaller than, equal to, or greater than the second parameter.
Equals (obj) method: returns true only when the specified object is also a Comparator and forcibly implements the same sorting as this Comparator.
 The second parameter of Collections. sort (list, new PriceComparator (); returns an int value, which is equivalent to a flag, telling the sort method in what order to sort the list. Example: 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> (); // array sequence Book b1 = new Book (10000, "Dream of Red Mansions", 150.86, new GregorianCalendar (2009, 01, 25), "Cao Xueqin, Gao e "); book b2 = new Book (10001, "Romance of the Three Kingdoms", 99.68, new GregorianCalendar (2008, 7, 8), "Luo Guanzhong"); Book b3 = new Book (10002, "Water Margin", 100.8, new GregorianCalendar (2009, 6, 28), "Shi Nai"); Book b4 = new Book (10003, "Journey to the West", 120.8, new GregorianCalendar (2011, 6, 8), "Wu chengen"); Book b5 = new Book (10004, "tianlong Babu", 10.4, new GregorianCalendar (2011, 9, 23), "Sohu "); list. add (b1); list. add (b2); list. add (b3); list. add (b4); list. add (b5); // Collections. sort (list); // No Default comparator, cannot sort System. out. println ("elements in the array sequence:"); myprint (list); Collections. sort (list, new PriceComparator (); // sort System by price. out. println ("sort by book price:"); myprint (list); Collections. sort (list, new CalendarComparator (); // sort System by time. out. println ("sort by Book Publishing Time:"); myprint (list) ;}// custom comparator: sort by Book price static class PriceComparator implements Comparator {public int compare (Object object1, Object object2) {// method Book p1 = (Book) object1 in the implementation interface; // forcibly convert Book p2 = (Book) object2; return new Double (p1.price ). compareTo (new Double (p2.price) ;}// custom Comparator: sort by book publication time static class CalendarComparator implements Comparator {public int compare (Object object1, Object object2) {// method Book p1 = (Book) object1 in the implementation interface; // forcibly convert Book p2 = (Book) object2; return p2.calendar. compareTo (p1.calendar) ;}// custom method: The Branch prints out the public static void myprint (list <Book> List) {Iterator it = list. iterator (); // get iterator, used to traverse all elements in the list while (it. hasNext () {// returns true System if the iterator contains elements. out. println ("\ t" + it. next (); // display this element}
}
} RewriteThe compare method can also add columns  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.