package com.list; import java.util.ArrayList; import java.util.Comparator; import java.util.List; public class CustomComparator { public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add("a"); list.add("c"); list.add("b"); for (String str : list) { System.out.println("---1---"+str); } list.sort(new Comparator<String>() { @Override public int compare(String o1, String o2) { int compareTo = o1.compareTo(o2); return compareTo; } }); for (String str : list) { System.out.println("---2---"+str); } } }
结果
---1---a
---1---c
---1---b
---2---a
---2---b
---2---c