Here you can find the source of sortByLength(List
public static void sortByLength(List<String> list, final boolean ascending)
//package com.java2s; //License from project: Apache License import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main { public static void sortByLength(List<String> list, final boolean ascending) { Comparator<String> c = new Comparator<String>() { public int compare(String e1, String e2) { int length1 = e1.length(); int length2 = e2.length(); if (ascending) return Double.compare(length1, length2); else return Double.compare(length2, length1); }//from w w w. j ava 2 s . c o m }; Collections.sort(list, c); } }