Pankaj Kumar
Pankaj Kumar

Reputation: 83008

Implementation of sorting in Vector

I have a Collection as

Vector<HashMap<String, String>>

Actually I am using this as list items of list view in android.I used SortedMap but I did not get correct result. I think this is because HashMap's structure is

hashMap.add("Name","value_of_name"); 
hashMap.add("Counts","value_of_counts");

Now I am adding it to Vector.

I want to sort vector's element by Name key of hashMap. I know about Collection.sort and I can sort this using ArrayList and POJO class. But I do not know how to use it with adapter of my ListView

How can I sort elements. And is there any better solution (Regarding my data-structure of collection, which can be use with adapter easily) ?

Upvotes: 0

Views: 901

Answers (5)

justkt
justkt

Reputation: 14766

If you want to sort the maps in the array, use a SortedMap implementation like TreeMap or ConcurrentSkipListMap. This takes a vector of HashMaps and returns a ArrayList (a non-synchronized and faster collection than Vector) of SortedMaps.

public ArrayList<SortedMap<String, String>> sortMaps(Vector<HashMap<String, String> maps) {
    ArrayList<TreeMap<String, String>> returnMaps = new ArrayList<TreeMap<String, String>>();
    for(HashMap<String, String> theMap : maps) {
        // TreeMap is a sorted map and this will use the default String.compareTo
        TreeMap<String, String> newMap = new TreeMap<String, String>();
        // put all the items from the HashMap into the TreeMap, which will autosort
        newMap.putAll(theMap);
        returnMaps.add(newMap);
    }
    return returnMaps;
}

To sort the Vector by the first keys (lowest keys, first alphabetically) of the hash map try the following before the return line:

    // this sorts the vector by first keys
    Collections.sort(returnMaps, new Comparator<SortedMap<String,String>>() {
        public int compare(SortedMap<String,String> a, HashMap<String,String> b) {
            return a.firstKey().compareTo(b.firstKey());
        }
    });

Or if you want to sort by last key (highest keys, last alphabetically):

    // this sorts the vector by first keys
    Collections.sort(returnMaps, new Comparator<SortedMap<String,String>>() {
        public int compare(SortedMap<String,String> a, HashMap<String,String> b) {
            return a.lastKey().compareTo(b.lastKey());
        }
    });

To return one sorted map of all keys (will stomp on any duplicates):

public SortedMap<String, String> singledSortedMap(Vector<HashMap<String, String> maps) {
    // this will end up with all the values, sorted by natural string ordering
    SortedMap<String, String> returnMap = new TreeMap<String, String>();
    for(HashMap<String, String> theMap : maps) {
        returnMap.putAll(theMap);
    }
    return returnMap;
}

Upvotes: 0

Roy van Rijn
Roy van Rijn

Reputation: 850

The best (fastest) way it to use a TreeMap instead. If you supply it with the correct Comperator all the items in the TreeMap will be sorted.

The important question: Why do you have a Vector of HashMaps?

Upvotes: 0

Riduidel
Riduidel

Reputation: 22300

Have you never thought about taking a look at collections in java.util package ?

You would then have discovered that Treemap already implements balanced tree sorting for Comparable items, like String is.

So, to have your items sorted, just repalce your HashMap with a TreeMap, and all the work will be done.

BTW what does this vector does here ? They're sooo Java 1.1 (fifteen years old, in other words)

Upvotes: 1

MAK
MAK

Reputation: 26586

You need to a implement a Comparator<HashMap<String,String> > and place the logic of your sort ordering inside its compare method.

Upvotes: 2

Maurice Perry
Maurice Perry

Reputation: 32831

Not sure I understand correctly. This will sort the vector on one key of the maps.

Collections.sort(yourVector, new Comparator<HashMap<String,String>>() {
    public int compare(HashMap<String,String> a, HashMap<String,String> b) {
        return a.get(yourKey).compareTo(b.get(yourKey));
    }
});

Upvotes: 1

Related Questions