If you want to sort the map based on Values .
Below is the sample code
Example : In the below example key is student name , Value is Student marks
Map<String, Integer> studentToTotal = new TreeMap<String, Integer>();
If you want to sort the studentname(key) based on mark(value) . First you have to convert the mapbject
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(
studentToTotal.entrySet());
then use Collections sort method
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
} );
The above method will sort the student name based on highest total to lowest total.
If you want to sort the student name based on lowest totatl to highest total below is the code snippet
Collections.sort( list, 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());
}
} );
Example program to sort the student name based on highest total to lowest total
package com.awf.org;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.List;
public class SortingBasedOnMapValues {
public static void main(String[] args) {
// studentToTotal map key is student name ,value is student total
Map<String, Integer> studentToTotal = new TreeMap<String, Integer>();
List<Entry<String, Integer>> sortedlistdsc = new ArrayList<Entry<String, Integer>>();
studentToTotal.put("awf-Tamil", 1000);
studentToTotal.put("awf-Tirumala", 986);
studentToTotal.put("awf-Joel", 999);
studentToTotal.put("awf-Rahul", 984);
studentToTotal.put("awf-Arun", 987);
sortedlistdsc=sortStudentsBasedOnTotal(studentToTotal);
System.out.println("Student details based on highest to lowest Total ");
for (Entry<String, Integer> entry : sortedlistdsc{
System.out.println("Name ::" + entry.getKey()
+ "\tTotal ::" + entry.getValue() + "\n");
}
}
public static List<Entry<String, Integer>> sortStudentsBasedOnTotal(
Map<String, Integer> studentToTotalref) {
// Sort the cells based on Student marks
// put the studenttotal map entry set in list using the below command for sorting
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(
studentToTotalref.entrySet());
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
} );
return list;
}
}
=======================================================================
Output :
Student details based on highest to lowest Total
Name ::awf-Tamil Total ::1000
Name ::awf-Joel Total ::999
Name ::awf-Arun Total ::987
Name ::awf-Tirumala Total ::986
Name ::awf-Rahul Total ::984
Regards,
R Tamilanban M.E(CSE)
AWF Community
No comments:
Post a Comment