|
发表于 2020-4-14 09:00:01
|
显示全部楼层
//: c11:DirList.java
// Displays directory listing.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package org.luyang.collections;
import java.util.Arrays;
import java.util.Comparator;
public class Sort {
public static String sort1 = "BIG_TO_SMALL";
public static String sort2 = "SMALL_TO_BIG";
public static void main(String[] args) {
String[] list = new String[] { "C1", "A2", "B1", "E1", "D1", "F1" };
Arrays.sort(list, new AlphabeticComparator1(sort2));
for (int i = 0; i < list.length; i++)
System.out.println(list[i]);
}
}
class AlphabeticComparator1 implements Comparator {
String sort = null;
public AlphabeticComparator1(String sort) {
this.sort = sort;
}
public int compare(Object o1, Object o2) {
String s1 = (String) o1;
String s2 = (String) o2;
if ("SMALL_TO_BIG".equalsIgnoreCase(sort)) {
return s1.toLowerCase().compareTo(s2.toLowerCase());
} else {
return s1.toLowerCase().compareTo(s2.toLowerCase()) * -1;
}
}
} // /:~ |
|