/** * @author lautturi.com * Java example:sorting a list in java */ import java.util.*; import java.util.stream.Collectors; public class Lautturi { public static void main(String[] args) { List<String> list = Arrays.asList( "hello","lautturi","java","python","world","lau","perl","js"); System.out.println("original list:"+list); // list = list.stream().sorted().collect(Collectors.toList()); // sorting list using stream method Collections.sort(list); // sort the list in increasing/ascending/natural order by default // Collections.sort(list, Collections.reverseOrder()); // decreasing/descending/reverse order // Collections.sort(list, (a, b) -> a.compareToIgnoreCase(b)); // sort ignore the char case of string System.out.println("sorted list:"+list); } }
output:
original list:[hello, lautturi, java, python, world, lau, perl, js] sorted list:[js, perl, lau, world, python, java, lautturi, hello]