package trenator.basic2; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Comparator; public class CompareDemo { /* * comparable接口的使用方法 */ public static void main(String[] args){ int a="Tom".compareTo("Tom");//比较第一个字母 System.out.println("a="+a); ///////////////////////////////////// List<String> names=new ArrayList<String>(); names.add("Tom"); names.add("Jerry"); names.add("Black"); names.add("Andy"); names.add("Lee"); System.out.println(names); // Collections.sort(names); //排序1 Bylength comparator=new Bylength(); Collections.sort(names,comparator);//排序2 System.out.println(names); } } class Bylength implements Comparator<String>{ public int compare(String str1,String str2){ return str1.length()-str2.length(); } }