public static Hashtable<String,Integer> getTablesInfo() {
Hashtable<String,Integer> tables = new Hashtable<String,Integer>();
tables.put("AAA", 5);
tables.put("BBB", 4);
tables.put("CCC", 7);
tables.put("DDD", 2);
tables.put("EEE", 1);
return tables;
}
public static void sort(Hashtable<String, Integer> t){
//Transfer as List and sort it
ArrayList<Map.Entry<String, Integer>> l = new ArrayList(t.entrySet());
Collections.sort(l, new Comparator<Map.Entry<String, Integer>>(){
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}});
System.out.println(l);
}
public static void main (String[] args) {
Hashtable<String,Integer> tables = getTablesInfo();
sort(tables);
}
Hashtable<String,Integer> tables = new Hashtable<String,Integer>();
tables.put("AAA", 5);
tables.put("BBB", 4);
tables.put("CCC", 7);
tables.put("DDD", 2);
tables.put("EEE", 1);
return tables;
}
public static void sort(Hashtable<String, Integer> t){
//Transfer as List and sort it
ArrayList<Map.Entry<String, Integer>> l = new ArrayList(t.entrySet());
Collections.sort(l, new Comparator<Map.Entry<String, Integer>>(){
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}});
System.out.println(l);
}
public static void main (String[] args) {
Hashtable<String,Integer> tables = getTablesInfo();
sort(tables);
}
Reference:
[1] How to sort a Java Hashtable?
沒有留言:
張貼留言