1. 程式人生 > >TreeMap指定排序器進行排序

TreeMap指定排序器進行排序

Map<String, String[]> sortedHashMap = new TreeMap<String, String[]>(new Comparator<String>(){  
          
		public int compare(String o1, String o2) {  
		  
			//指定排序器按照降序排列(o1, o2指的是Map的Key值)  
			char char1 = o1.charAt(0);
			char char2 = o2.charAt(0);
			int compare = Integer.valueOf(char2)-Integer.valueOf(char1);
			if (compare==0) {
				return o2.substring(1).compareTo(o1.substring(1));
			}            	
			return compare;  
		}     
	});