31fps by Sam Purtill A blog about business, technology, and life

22Jul/072

One of the many reasons why I still hate Java

Ok here is the Python that I wrote:

word_list = words.split(', ')
word_list.sort(lambda x, y: len(y)-len(x))
for word in word_list: print word

And here is the Java:


import java.util.Comparator;
import java.util.Arrays;
class LengthComparer implements Comparator {
public int compare(Object a, Object b) {
return ((String)b).length() - ((String)a).length();
}
}
public class JavaTest {
public static void main(String[] args) {
String words = "java, python";
String[] word_list = words.split(", ");
Arrays.sort((Object[])word_list, new LengthComparer());
for(int i = 0; i < word_list.length; i++)
System.out.println(word_list[i]);
}
}

Indentations will probably be lost in the HTML, oh well. Anyways.

Oh someone just told me a way to make the Python even shorter:

print "\n".join(sorted(words.split(", "), lambda x, y: len(y) - len(x)))

And I couldn't resist putting this picture up from ICANHASCHEEZBURGER, my favorite place on the internet... This made me think of Java (it also made me laugh hysterically)

NO PAIN NO GAIN

Filed under: Python Leave a comment
Comments (2) Trackbacks (0)
  1. so the point is python

  2. stupid html …
    so the point is python < java ? ;- )
    and that pic is great :- D


Leave a comment


No trackbacks yet.

Blogroll

Archive

Meta