-4

I have the below HashMap:

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

Which uses the following keys:

static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_TITLE = "title";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";
static final String KEY_LINK = "link";
static final String KEY_PUBDATE = "pubDate";
static final String KEY_TIME = "pubDate";

I then use this piece of code, in order to populate the hashmap.

HashMap<String, String> map = new HashMap<String, String>();

map.put(KEY_ID, parser.getValue(e, KEY_ID));
//Get the title of the article.
map.put(KEY_NAME, parser.getValue(e, KEY_TITLE));
//Get the description of the article.
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
//Add the keys to the hashmap.
menuItems.add(map);

etc.....

I have a key called KEY_TIME which houses the publish time for each article in the format 16:30 for example.

I wish to know how I would sort this hashmap, by the key KEY_TIME, so that the artcicles at the top would be the most recent, and the ones at the bottom would be the oldest.

I know this has been asked before, but I can't find a solution which accounts for my ArrayList before the Hashmap.

stealthjong
  • 10,858
  • 13
  • 45
  • 84
Callum
  • 21
  • 4
  • 1
    please do some research befor asking ... it was asked here many times and i'm pretty sure that google will give you some answers – Selvin Aug 29 '14 at 08:37
  • 3
    Any reason why you did not use TreeMap? It is already sorted. – Christian Kullmann Aug 29 '14 at 08:38
  • i'm pretty sure that he wants to get this `ArrayList> menuItems` (by the one of its value - like hashmapItem["Column"]) not HashMaps itself ... i'm pretty sure that TreeMap orders by the key in the same instance of TreeMap not multiple TreeMaps in the ArrayList – Selvin Aug 29 '14 at 08:39
  • I have tried using TreeMap, but I found some problems converting my hashmap to a treemap. However, I will take another look into it, thanks. – Callum Aug 29 '14 at 08:39
  • 1
    If you really need to stick to ArrayList (or List in general) for this, one option would be to create a dataholder class and make it comparable by KEY_TIME (instead of using a Map). – Christian Kullmann Aug 29 '14 at 08:41
  • I wonder why you use hashmap instead of a proper class. – holap Aug 29 '14 at 08:43
  • i'm pretty sure that he is using it becuase of SimpleAdapter ... – Selvin Aug 29 '14 at 08:45

1 Answers1

1

If I get your question correct since you want item details added in individual hashmaps to be sorted based on the item add time then you actually have to sort the arraylist which contains the collection of items and not the individual hashmaps. You can do this by creating a custom comparator class as follows:-

class ItemEntryComparator implements Comparator<Map<String,String>> {

    static final String KEY_TIME = "pubDate";
    @Override
    public int compare(Map<String, String> o1, Map<String, String> o2) {        
        return o2.get(KEY_TIME).compareTo(o1.get(KEY_TIME));
    }

}

This comparator can be passed to the collections sort to sort your items arraylist:-

Collections.sort(menuItems, new ItemEntryComparator());
Manjunath
  • 1,685
  • 9
  • 9