Java Implementation Map Collection sorting function

Source: Internet
Author: User

First step: New sort temporary key in map

Initialize Map collection list<map<string, string>> columns = new arraylist<map<string, string>> (); map<string, string> c1 = new hashmap<string,string> (); C1.put ("Sort", "8"); C1.put ("title", " Www.chuweibiao.com "); Columns.Add (C1); map<string, string> c2 = new hashmap<string,string> (); C2.put ("Sort", "5"); C2.put ("title", " Www.chuweibiao.com "); Columns.Add (C2); Map<string, string> c3 = new hashmap<string,string> (); C3.put ("Sort", "all"); C3.put ("title", " Www.chuweibiao.com "); Columns.Add (C3);

Step Two: Sort the

/** * Map Type element Collection Sort * @param columns * Map type element collection */private void Listsortingformaptypeelement (list<map<string, object& gt;> columns) {collections.sort (columns, New comparator<map<string, object>> () {public int compare (MAP <string, Object> last, map<string, object> next) {Object lastsort = Last.get ("sort"); object nextsort = next.ge T ("sort"), if (Lastsort = = NULL | | nextsort = = NULL) {return 0;} else {return Integer.parseint (string.valueof (lastsort)) &G T Integer.parseint (string.valueof (nextsort))? 1:0;}});}

Step three: Remove temporary keys

/** * Remove sort temporary key * @param columns * MAP type element collection */private void Removesortkey (list<map<string, object>> columns) {fo R (map<string, object> column:columns) {column.remove ("sort");}}

who has a better way to make brick advice, O (∩_∩) o Thank you ~


Java Implementation Map Collection sorting function

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.