Implementation of list custom sorting and inverted sorting in java

Source: Internet
Author: User

Knowledge points involved: if the title owner can smile ~~~ Data is processed today. The processed data is stored in List (). The elements in the list are HashMap <String, Object>. The element map contains three key-value pairs: id, name, num. it mainly deals with the number of occurrences of each element, and the subsequent num is the number of occurrences. The requirement is to put the data that appears more frequently at the front. You need to write a comparison class to implement the java. util. Comparator interface. Use Collections. sort (list, comparator); for comparison. Code: ① [java] public static void main (String [] args) {List <HashMap <String, Object> list = new ArrayList <HashMap <String, object >>> (); HashMap <String, Object> map = new HashMap <String, Object> (); map. put ("name", "gjk"); map. put ("id", "1"); map. put ("num", 45); list. add (map); HashMap <String, Object> map2 = new HashMap <String, Object> (); map2.put ("name", "gjk2"); map2.put ("id ", "2"); map2.put ("num", 60); List. add (map2); HashMap <String, Object> map3 = new HashMap <String, Object> (); map3.put ("name", "gjk3"); map3.put ("id ", "3"); map3.put ("num", 30); list. add (map3); // print the value in the list // printList (list); ComparatorHashMap comparator = new ComparatorHashMap (); Collections. sort (list, comparator); System. out. println ("after @"); // print the value in the list. // printList (list);} The comparison class is as follows, you can make changes based on your business needs: ② [java] public Class ComparatorHashMap implements Comparator {public int compare (Object arg0, Object arg1) {HashMap <String, Object> map = (HashMap <String, Object>) arg0; HashMap <String, object> map2 = (HashMap <String, Object>) arg1; // compares the number of occurrences. If the number is the same, the comparison name Integer num = Integer. parseInt (map. get ("num "). toString (); Integer num2 = Integer. parseInt (map2.get ("num "). toString (); int flag = num2.compareTo (num); if (flag = 0) {return (map2.get ("name "). toString ()). compareTo (map. get ("name "). toString () ;}else {return flag ;}③ sorting involves both positive and inverted sorting, which must be processed in your own implementation class. If it is positive sorting, compare the first value and the second value; if it is inverted sorting, compare the second value and the first value, and the two are reversed.

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.