From: laredotornado on
Hi,

Does anyone know how to sort a map.entrySet by the value? Each entry
is an Integer mapped to a String, but I wish to have the whole thing
sorted alphabetically by the strings. Any ideas how I can do this?
Below is the function that ultimately serves up the map.entrySet.
(The Map[] object is a list returned from a database).

Thanks, - Dave


@ModelAttribute("promoEntryTypes")
public Set<Map.Entry<String, String>>
populatePromoEntryTypes(@ModelAttribute("appInterface")
JspAppInterfaceAdmin appInterface) {
final Map<String, String> map = new TreeMap<String, String>();
try {
if (appInterface != null) {
Map[] promoEntryTypes = appInterface.getUserPromotionTypes();
for (int i=0; i<promoEntryTypes.length; i++) {
map.put(((Integer) promoEntryTypes[i].get("id")).toString(),
(String) promoEntryTypes[i].get("promotion_name"));
} // for
} // if
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return map.entrySet();
}
From: Roedy Green on
On Fri, 23 Apr 2010 06:51:15 -0700 (PDT), laredotornado
<laredotornado(a)zipmail.com> wrote, quoted or indirectly quoted someone
who said :

>Does anyone know how to sort a map.entrySet by the value? Each entry
>is an Integer mapped to a String, but I wish to have the whole thing
>sorted alphabetically by the strings.

Generally it is simpler to sort just before you need the data is
order. See http://mindprod.com/jgloss/sort.html

If you need the sorted order all the time use a TreeSet or its brother
TreeMap.

http://mindprod.com/jgloss/treemap.html
http://mindprod.com/jgloss/treeset.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

It�s amazing how much structure natural languages have when you consider who speaks them and how they evolved.
From: laredotornado on
On Apr 23, 9:15 am, Roedy Green <see_webs...(a)mindprod.com.invalid>
wrote:
> On Fri, 23 Apr 2010 06:51:15 -0700 (PDT), laredotornado
> <laredotorn...(a)zipmail.com> wrote, quoted or indirectly quoted someone
> who said :
>
> >Does anyone know how to sort a map.entrySet by the value?  Each entry
> >is an Integer mapped to a String, but I wish to have the whole thing
> >sorted alphabetically by the strings.
>
> Generally it is simpler to sort just before you need the data is
> order. Seehttp://mindprod.com/jgloss/sort.html
>
> If you need the sorted order all the time use a TreeSet or its brother
> TreeMap.
>
> http://mindprod.com/jgloss/treemap.htmlhttp://mindprod.com/jgloss/treeset..html
> --
> Roedy Green Canadian Mind Productshttp://mindprod.com
>
> It’s amazing how much structure natural languages have when you consider who speaks them and how they evolved.


I do use a TreeMap -- "final Map<String, String> map = new
TreeMap<String, String>();", but the order is not getting preserved
when I call "map.entrySet". - Dave
From: Mike Schilling on
laredotornado wrote:
> On Apr 23, 9:15 am, Roedy Green <see_webs...(a)mindprod.com.invalid>
> wrote:
>> On Fri, 23 Apr 2010 06:51:15 -0700 (PDT), laredotornado
>> <laredotorn...(a)zipmail.com> wrote, quoted or indirectly quoted
>> someone
>> who said :
>>
>>> Does anyone know how to sort a map.entrySet by the value? Each entry
>>> is an Integer mapped to a String, but I wish to have the whole thing
>>> sorted alphabetically by the strings.
>>
>> Generally it is simpler to sort just before you need the data is
>> order. Seehttp://mindprod.com/jgloss/sort.html
>>
>> If you need the sorted order all the time use a TreeSet or its
>> brother
>> TreeMap.

>
> I do use a TreeMap -- "final Map<String, String> map = new
> TreeMap<String, String>();", but the order is not getting preserved
> when I call "map.entrySet".

A SortedMap (like TreeMap) is sorted by keys, not values. (And you said
earlier that the keys are Integers, but here they're Strings, so I'm
confused.)


From: Tom Anderson on
On Fri, 23 Apr 2010, laredotornado wrote:

> Does anyone know how to sort a map.entrySet by the value? Each entry is
> an Integer mapped to a String, but I wish to have the whole thing sorted
> alphabetically by the strings. Any ideas how I can do this? Below is
> the function that ultimately serves up the map.entrySet. (The Map[]
> object is a list returned from a database).

SortedSet<Map.Entry<String, String>> results = new TreeSet<Map.Entry<String, String>>(new Comparator<>(<Map.Entry<String, String>>) {
public int compare(<Map.Entry<String, String> a, <Map.Entry<String, String> b) {
int d = a.getValue().compareTo(b.getValue());
if (d == 0) d = a.getKey().compareTo(b.getKey());
return d;
}

});
results.addAll(map.entrySet());
return results;

Note that this assumes none of your values are null, and that no pair of
entries with the same value has any null keys.

tom

--
Per Dementia ad Astra