import java.util.ArrayList; import java.util.Comparator; import java.util.List; import perlforjava.control.Looper; import perlforjava.functions.Arrays; import perlforjava.functions.Mapper; import perlforjava.lang.Block; public class TheSchwartz { public static void main(String[] args) { // Create a new List of values to sort ArrayList in = new ArrayList(); in.add("foobarbazquux"); in.add("foo"); in.add("foobarbaz"); in.add("foobar"); /* Sort based upon length of string by mapping each string to a List where the first element is the string and the second is its length. Sort on the latter and return a new List of strings. */ List out = Mapper.map( new Block() { // pull the first element from each List and return it protected Object process(Object elem) { return ((List)elem).get(0); } }, Arrays.sort( // run the comparison of Integer values to determine order new Comparator() { public int compare(Object a, Object b) { return ((Integer)((List)a).get(1)). compareTo(((Integer)((List)b).get(1))); } }, // map each string into an ArrayList of string => length Mapper.map( new Block() { protected Object process(Object elem) { ArrayList temp = new ArrayList(); temp.add(elem); temp.add(new Integer(((String)elem).length())); return temp; } }, in ) ) ); // Loop over the results to view them Looper.foreach( out, new Block() { protected Object process(Object elem) { System.out.println("ELEM " + elem.toString()); return null; } } ); } }