- import java.util.*;
- class Demo
- {
- public static void main(String[] args)
- {
- DemoSort();
- }
- public static void DemoSort()
- {
- List<String> ls=new ArrayList<String>();
- ls.add("aa");
- ls.add("bca");
- ls.add("d");
- ls.add("abcdf");
- sop(ls);
- //Collections.sort(ls);
- Collections.sort(ls,new sortLength());
- sop(ls);
- }
-
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- }
- class sortLength implements Comparator<String>
- {
- public int compare(String s1,String s2)
- {
- if(s1.length()>s2.length())
- return 1;
- if(s1.length()<s2.length())
- return -1;
- return s1.compareTo(s2);
- }
- }
复制代码
|
|