List (Map (String, Object) Chinese field sorting

Source: Internet
Author: User

List (Map (String, Object) Chinese field sorting

Loop traversal is sorted by a specific Chinese field

/*** Chinese sorting ** @ author itmyhome **/public class MapComparator implements Comparator
  
   
>{@ Override public int compare (Map
   
    
O1, Map
    
     
O2) {// TODO Auto-generated method stub String b1 = (String) o1.get (NAME); String b2 = (String) o2.get (NAME); Collator cmp = Collator. getInstance (java. util. locale. CHINA); if (cmp. compare (b1, b2)> 0) {return 1;} else if (cmp. compare (b1, b2) <0) {return-1 ;}return 0 ;}}
    
   
  
Import java. util. arrayList; import java. util. collections; import java. util. hashMap; import java. util. list; import java. util. map; public class Test {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub List
  
   
> List = new ArrayList
   
    
> (); Map
    
     
Map1 = new HashMap
     
      
(); Map
      
        Map2 = new HashMap
       
         (); Map
        
          Map3 = new HashMap
         
           (); Map
          
            Map4 = new HashMap
           
             (); Map
            
              Map5 = new HashMap
             
               (); Map1.put (NAME, Zhu Yuanzhang); map2.put (NAME, often encounter spring); map3.put (NAME, Zhang Wuji); map4.put (NAME, Guo Jing); map5.put (NAME, Hong qigong); list. add (map1); list. add (map2); list. add (map3); list. add (map4); list. add (map5); System. out. println (*** before sorting **); for (Map
              
                Map: list) {System. out. println (map. get (NAME);} System. out. println (*** sorted ***); Collections. sort (list, new MapComparator (); for (Map
               
                 Map: list) {System. out. println (map. get (NAME ));}}}
               
              
             
            
           
          
         
        
       
      
     
    
   
  

Result:

* ** Before sorting, ** Zhu Yuanzhang Changyu, Zhang Wuji, Guo Jing, Hong Qigong, *** after sorting, *** Changyu, Guo Jing, Hong Qigong, Zhang Wuji, Zhu Yuanzhang

 

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.