ayeen c
ayeen c

Reputation: 71

how to sort a vector of hashmaps

i have a Vector of Hashmap and the HashMap contains different data types:

Vector<HashMap<String, Object>> theVector= new Vector<HashMap<String, Object>>();

theResults contains this HashMap:

HashMap<String, Object> theHashMap= new HashMap<String, Object>();

theHashMap has these data: (pretend this is a for-loop)

//1st set

theHashMap.put("BLDG_ID", 111); //int
theHashMap.put("EMP_NAME", "AAA"); //String
theHashMap.put("FLAG", true); //boolean

theVector.add(theHashMap);

//2nd set

theHashMap.put("BLDG_ID", 222); //int
theHashMap.put("EMP_NAME", "BBB"); //String
theHashMap.put("FLAG", false); //boolean

theVector.add(theHashMap);

//2nd set<br>
theHashMap.put("BLDG_ID", 111); //int
theHashMap.put("EMP_NAME", "CCC"); //String
theHashMap.put("FLAG", false); //boolean

theVector.add(theHashMap);

I want to sort the contents of my vector of HashMap according to BLDG_ID so that when I display the data it would look like

BLDG_ID || EMP_NAME
111     ||    AAA
111     ||    CCC
222     ||    BBB

How do I do that?

Upvotes: 0

Views: 3206

Answers (3)

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

List<Map<String, Object>> vector = new Vector<Map<String, Object>>();

Collections.sort(vector, new Comparator<Map<String, Object>>() {
    @Override
    public int compare(Map<String, Object> map1, Map<String, Object> map2) {
        return ((Integer) map1.get("BLDG_ID")).compareTo((Integer) map2.get("BLDG_ID")));
    }            
});

Update: For your code:

After the "last"

theVector.add(theHashMap);

add the following

Collections.sort(theVector, new Comparator<HashMap<String, Object>>() {
        @Override
        public int compare(HashMap<String, Object> o1, HashMap<String, Object> o2) {
            return ((Integer) o1.get("BLDG_ID")).compareTo((Integer) o2.get("BLDG_ID"));
        }            
    });

Upvotes: 1

corsiKa
corsiKa

Reputation: 82589

I think you'd be much better off doing something like this: Instead of using a hashmap for your values, just make a class. Then you'll get compile time checking on your operations, which will help prevent errors down the road.

class Employee implements Comparable<Employee> {
    int buildingId;
    String name;
    boolean flag;

    Employee(int b, String n, boolean f) {
        buildingId = b;
        name = n;
        flag = f;
    }

    public int compareTo(Employee other) {
        if(other.buildingId == this.buildingId) 
            return name.compareTo(other.name);
        return buildingId - other.buildingId; // potential for overflow, be careful
    }

}

Then you can just sort the vector using whatever sort you want. If you use ArrayList (the modern form of Vector) you can use Collections.sort(myList);

List<Employee> emps = new ArrayList<Employee>();
emps.add(new Employee(111,"AAA",true));
emps.add(new Employee(111,"CCC",false));
emps.add(new Employee(111,"BBB",false));

Collections.sort(emps);
System.out.println("Building Id,Employee Name");
for(Employee emp : emps) System.out.println(emp.getCSV()); // or however you want to format it

Upvotes: 2

Puce
Puce

Reputation: 38142

Implement a custom Comparator<Map<String, Object>>, then call Collections.sort

Note: You might want to use ArrayList instead of Vector.

Upvotes: 1

Related Questions