<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Comments on snippet: 'Sorting map keys by comparing it's values'</title>
<link>http://snipplr.com</link>
<description>Snipplr comments feed'</description>
<language>en-us</language>
<pubDate>Thu, 23 May 2013 22:44:25 GMT</pubDate>
<item>
<title>hohonuuli said on 6/8/07</title>
<link>http://snipplr.com/view/2789/sorting-map-keys-by-comparing-its-values/</link>
<description><![CDATA[ <p>The equivalent Groovy code to the Java shown is actually:</p>

<pre><code>
// create Map 
def lang = [0:"Java", 1:"Groovy", 2:"Ruby", 3:"Python", 4:"C#", 5:"C++", 6:"Perl"] 
// Sort the keys by value 
def keys = lang.keySet().sort {lang[it]} 
// List the key value 
keys.each { println "${it} ${lang[it]}" }
</code></pre>

<p>Also, the Java code for Java 5 and 6 could be more like:</p>

<pre><code>
final Map lang = new HashMap() {{ 
    put(0, "Java"); put(1, "Groovy"); put(2, "Ruby"); 
    put(3, "Python"); put(4, "C#"); put(5, "C++"); 
    put(6, "Perl"); }}; 
List keys = new ArrayList(lang.keySet()); 
Collections.sort(keys, new Comparator() { 
    public int compare(Integer left, Integer right) { 
        return lang.get(left).compareTo(lang.get(right)) 
    } 
}); 
for (Integer k : keys) { System.out.println(k + " " lang.get(k); }
</code></pre>

<p>Groovy definitly requires WAY less scaffolding than Java. On the other hand Java is WAY WAY faster than groovy. (Nothing in life comes free ;-)</p>
 ]]></description>
<pubDate>Fri, 08 Jun 2007 09:45:32 GMT</pubDate>
<guid>http://snipplr.com/view/2789/sorting-map-keys-by-comparing-its-values/</guid>
</item>
<item>
<title>hohonuuli said on 6/8/07</title>
<link>http://snipplr.com/view/2789/sorting-map-keys-by-comparing-its-values/</link>
<description><![CDATA[ <p>The equivalent Groovy code to the Java shown is actually:</p>

<p>// create Map
def lang = [0:"Java", 1:"Groovy", 2:"Ruby", 3:"Python", 4:"C#", 5:"C++", 6:"Perl"]
// Sort the keys by value
def keys = lang.keySet().sort {lang[it]}
// List the key value
keys.each { println "${it} ${lang[it]}" }</p>

<p>Also, the Java code for Java 5 and 6 could be more like:</p>

<p>final Map lang = new HashMap() {{ 
        put(0, "Java"); put(1, "Groovy"); 
        put(2, "Ruby"); put(3, "Python"); put(4, "C#");
         put(5, "C++"); put(6, "Perl");
}};
List keys = new ArrayList(lang.keySet());
Collections.sort(keys, new Comparator() {
    public int compare(Integer left, Integer right) {
        return lang.get(left).compareTo(lang.get(right))
    }
});
for (Integer k : keys) { System.out.println(k + " " lang.get(k); }</p>

<p>Groovy definitly requires WAY less scaffolding than Java. On the other hand Java is WAY WAY faster than groovy. (Nothing in life comes free ;-)</p>
 ]]></description>
<pubDate>Fri, 08 Jun 2007 09:40:50 GMT</pubDate>
<guid>http://snipplr.com/view/2789/sorting-map-keys-by-comparing-its-values/</guid>
</item>
</channel>
</rss>