Shared publicly  - 
 
I want this StackOverflow answer to die. Permanently. I hate it. (The question is "how to sort a map by value.")

It breaks in several really confusing ways if you even look at it funny. If the backing map changes, it will break. If multiple keys map to the same value, it will break. If you call get on a key that isn't in the backing map, it will break. If you do anything whatsoever that would cause a lookup to happen on a key that isn't in the map -- a Map.equals call, containsKey, anything -- it will break with really weird stack traces.

I probably shouldn't get so worked up about it, but it's just such an insidiously bad approach. I almost want Guava to add a feature to address sorting maps by value just so people won't use this terrible approach.
11
2
Jadiel de Armas's profile photoLouis Wasserman's profile photoGideon Dresdner's profile photoXiaoyi Duan's profile photo
18 comments
 
That's...a fair criticism, but I might claim that on StackOverflow, less experienced programmers are much more likely than usual to take misleading answers as gospel truth.
 
Hm, didn't you explain all this in comments to that answer?
 
Some. But "how to sort a map by values" is probably the single Java question on SO I see repeated more than any other -- about every 2 weeks -- and someone always recommends this terrible answer.
 
how about a map that takes a comparator as a side argument and when you iterate through the data it uses the comparator to silently sort the data beforehand... ?
 
If only there were some means to comment on and otherwise edit things on that site so that you could share your opinion with others... sigh. One day, maybe.
 
Indeed, but people keep bringing this answer back to life, every ~2 weeks.
 
With enough points I believe you can edit.... also I was on a phone, thats totally my excuse!
 
Gah I just wasted 30+ mins because of that TreeMap answer..
 
Just saw this linked from that answer (comparing my votes to it). Is there a better way than doing my answer at http://stackoverflow.com/a/3420912/37193? It's sorta elegant, and sorta a really huge (non thread-safe) hack.

At least I'm over 50% of the votes, but still...
 
I would say that the only reliable way to do it is to dump the entries to a list, sort the entries, and put them into an insertion-ordered map (e.g. LinkedHashMap or ImmutableMap).
 
Louis, you're totally right, that answer should die. Somebody asked this question AGAIN and posted some code looks vaguely like that top-voted (bad) answer. Somebody posted an answer that "fixed" it, so that it works merely as poorly as that top-voted (bad) answer. Downvoted. Hm, let me try the Mjölnir dupe-hammer....
 
+Jadiel de Armas The answers to that question look correct to me, although I'd go further and say what you're trying to do is wrong and that mutating objects in sets reflects an underlying design flaw in what you're trying to do; I don't think that's an operation that should make sense or work at all.
Add a comment...