排序

1、collection          ---[1]

public static void main(String[] args) {
		Map<String, Info> map = new HashMap<>();
		
		Info info1 = new Info("xxxx", "hhhh", "1980-11-01");
		Info info2 = new Info("xxxx", "hhhh", "1983-11-22");
		Info info3 = new Info("xxxx", "hhhh", "1925-11-01");
		
		map.put("小杰杰", info1);
		map.put("你好", info2);
		map.put("你好1", info3);
		
		List<Map.Entry<String,Info>> list = new ArrayList<Map.Entry<String,Info>>(map.entrySet());
		
		Collections.sort(list, new Comparator<Map.Entry<String, Info>>() {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			public int compare(Entry<String, Info> o1, Entry<String, Info> o2) {
				try {
                    Date dt1 = format.parse(o1.getValue().getTime());
                    Date dt2 = format.parse(o2.getValue().getTime());
                    if (dt1.getTime() > dt2.getTime()) {
                        return 1;
                    } else if (dt1.getTime() < dt2.getTime()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
				return 0;
			};
		});
		
		for(Map.Entry<String,Info> mapping : list){ 
            System.out.println(mapping.getKey()+" : "+mapping.getValue()); 
		} 
	}

 

2、comparator

public Person{

  String id;
 
  String name;

  Company company

  private Set<Company> companies = new TreeSet<>(new Comparator<Company>
  {
        @Overrider
        public int compare(Company c1. Company c2){
       
              return Collator.getInstance(Locale.CHINESES).
          compare(c1.getCompanyName(),c2.getCompanyName())


        }

   }) ;

} 

 

 

 

 

 

 

 

 

 

========================================================================

[1] : https://blog.csdn.net/jiejiexiao/article/details/86164315

posted @ 2020-03-25 09:05  抽象Java  阅读(130)  评论(0编辑  收藏  举报