Comparing strings, sorting

Source: Internet
Author: User

Tag: SSI Cal iterator returns Force price ArrayList. com greater than

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 a//Integer name2 = integer.valueof (o2.get ("category") from your list. ToString ()); Name1 is the second name//return Name1.compareto (name2) from your list, return O1.get ("category"). toString (). CompareTo (O2.get ("category"). toString ());}); Comparator built-in functions, querydate are list arrays that need to be arranged, followed by collations, Compare (A, B) method: Returns a negative integer, 0, or a positive integer, respectively, based on the first parameter less than, equal to, or greater than the second parameter.
Equals (obj) method: Returns True only if the specified object is also a Comparator and enforces the same sort as this Comparator.
Collections.sort (list, new Pricecomparator ()); The second parameter returns a value of type int, which is equivalent to a flag that tells the sort method 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& Gt List = new arraylist<book> ();   Array sequence Book B1 = new book (10000, "Red Mansions", 150.86, New GregorianCalendar (2009, 01, 25), "Cao Xueqin, Gao E");   Book b2 = new book (10001, "kingdoms", 99.68, New GregorianCalendar (2008, 7, 8), "Luo Guan Zhong");   Book B3 = new book (10002, "Outlaws of the Marsh", 100.8, New GregorianCalendar (2009, 6, 28), "Shi Naian");   Book B4 = new book (10003, "Journey to the Monkey", 120.8, New GregorianCalendar (2011, 6, 8), "Wu Chengen");   B5 = new book (10004, "Tianlong Eight", 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);   Without a default comparer, you cannot sort System.out.println ("elements in the array sequence:"); Myprint (list);   Collections.sort (list, new PrIcecomparator ());   Sort by price System.out.println ("Sort by book Price:"); Myprint (list);   Collections.sort (list, new Calendarcomparator ());   Sort by Time System.out.println ("Sort by book's publication Time:"); Myprint (list); }  //Custom comparators: Sort by price of book    static class Pricecomparator implements Comparator {public int compare (obje     CT object1, Object object2) {//Implements the method in the interface book P1 = (book) Object1;//cast book P2 = (book) Object2;      return new double (p1.price). CompareTo (new double (P2.price)); }}  //Custom Comparator: Sort static class Calendarcomparator by book publication Time implements Comparator {public int compare (Object Object1, Object object2) {//implement method in interface book P1 = (book) Object1;//cast book P2 = (book) Object2; return P2.calen     Dar.compareto (P1.calendar); }}  //Custom method: Branch Print output list element public static void Myprint (List<book> list) {Iterator it = List.iterator (); Gets an iterator that iterates through all the elements in the list while (It.hasnext ()) {//returns True if there are elements in the iterator System.out.prinTLN ("\ T" + it.next ());//Display the element   }
} override The Compare method can also add a number of columns  

Comparing strings, sorting

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.