Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Way to sort / enforce order for Map.entrySet?

Thread Tools

Way to sort / enforce order for Map.entrySet?

Mike Schilling
Posts: n/a
Mike Schilling wrote:
> Lew wrote:
>> Mike Schilling wrote:
>>> Lew wrote:
>>>> Mike Schilling wrote:
>>>>> Lew wrote:
>>>>>> Feed a
>>>>>> man a meal and you feed him for an hour; send him to the diner
>>>>>> and you feed him for a lifetime.
>>>>> Build a man a fire, and he's warm for a night, set him on fire,
>>>>> and he's warm his whole life.
>>>>>>> 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".
>>>>>> And you didn't even provide an SSCCE! What is wrong with you?
>>>>> Since he wanted to sort by value, not key, he's correct -- the
>>>>> entry set won't be sorted the way he wanted.
>>>> Unless, of course, he uses a custom Comparator that does sort the
>>>> way he wants.
>>> Not Map.entrySet(); that always sorts by the map's keys. (You can
>>> *copy* the result of entrySet() and sort that copy however you like,
>>> of course.)

>> And that would be exactly the advice that several people gave him.

> My point is that it doesn't require an SSCCE to demonstrate the the
> entrySet() of a TreeMap isn't sorted by the values.

Particularly since (it belatedly occurs to me), there's no requirement that
the values be comparable to each other.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
XPath to enforce document order Phlip XML 8 03-18-2009 01:56 PM
How to enforce execution order? =?ISO-8859-1?Q?Sacha_Sch=E4r?= C++ 14 02-02-2006 08:41 PM
xsl:sort lang="es" modern vs. tradidional Spanish sort order nobody XML 0 06-01-2004 06:25 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM