The map is stored in the list in java, and it is sorted according to a field in the map

The map is stored in the list in java, and it is sorted according to a field in the map

E.g:

package com;

import java.util.ArrayList;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Com {

	/**
	 * @param args
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("id", "1");
		map1.put("name", "p");
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("id", "2");
		map2.put("name", "h");
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("id", "3");
		map3.put("name", "f");
		list.add(map1);
		list.add(map3);
		list.add(map2);
		//before sorting
		for (Map<String, Object> map : list) {
			
			System.out.println(map.get("id"));
		}
		Collections.sort(list, new Comparator<Map<String, Object>>(){

			   public int compare(Map<String, Object> o1, Map<String, Object> o2) {
			    String name1 =(String)o1.get("id");//name1 is the one taken from your list
			    String name2= (String)o2.get("id"); //name1 is the second name taken from your list    
			    return name1.compareTo(name2);  
		   }
			   
			  });
		// after sorting
    System.out.println("-------------------");
 for (Map<String, Object> map : list) {
			
			System.out.println(map.get("id"));
		}
		
	}

}
The result of running is:

1

3

2


-------------------

1

2

3


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325349752&siteId=291194637