数字使用直接比较大小来排序,String的话,使用compare()方法进行排序。
测试代码:
1.对字符串对象排序
@Test
public void test17() throws Exception {
List< String > stringList = new ArrayList<>();
stringList.add("haha");
stringList.add("123");
stringList.add("wq");
stringList.add("d");
stringList.add("rt");
stringList.add("uuk");
System.out.println("排序前:" + stringList);
Collections.sort(stringList, new Comparator< String >() {
@Override
public int compare(String lhs, String rhs) {
System.out.println("排序字符串:" + lhs + "," + rhs);
int i = lhs.compareTo(rhs);
System.out.println("排序结果" + i);
if ( i > 0 ) {
return 1;
<