Oops -- since the key values are Integer objects, should be something
like:

return key1.compareTo(key2);

On Feb 2, 6:21 am, DanH <[email protected]> wrote:
> An alternative is to put your HashMaps into a regular HashMap[] array
> and then use Arrays.sort.  Create an external Comparator class that is
> roughly:
> class MyComparator implements Comparator {
> public int compare(HashMap o1, HashMap o2) {
>    Integer key1 = (Integer) o1.get("age");
>    Integer key2 = (Integer) o2.get("age");
>    return key1 - key2;
>
> }
> }
>
> (This can just be an extra method and interface added to your current
> class, and then use your "this" pointer for the Comparator in sort().
> Or you can define and instantiate a new class.)
>
> On Feb 1, 10:12 pm, DanH <[email protected]> wrote:
>
> > You have a bunch HashMaps of different people, and you want to sort by
> > age?
>
> > You need to spin through the list of HashMaps and extract age from
> > each one, then insert the age and HashMap into, eg, a TreeMap, with
> > age being the key.
>
> > On Feb 1, 8:15 pm, Mystique <[email protected]> wrote:
>
> > > Ok, I just read but unclear (java newbie) about this one:
>
> > > ArrayList<HashMap<String,String>> list = new
> > > ArrayList<HashMap<String,String>>();
>
> > > // I have a loop collecting these value from a XML parser
> > > HashMap<String,String> map = new HashMap<String,String>();
> > > map.put("name",Tname);
> > > map.put("age",Tage);
> > > list.add(map);
>
> > > So let say I have test data, I want to sort the age in the ArrayList
> > > call "list":
> > > mary, 12
> > > john, 11
>
> > > Any sample to look at?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to