方法摘要
| |
int
| |
boolean
|
import java.util.Comparator;
public class ByWeightComparator implements Comparator {
/** * Compare two Trucks by weight. Callback for sort or TreeMap. * effectively returns a-b; orders by ascending weight * * @param pFirst first object a to be compared * * @param pSecond second object b to be compared * * @return +1 if a>b, 0 if a=b, -1 if a<b */ public final int compare ( Object pFirst, Object pSecond ) { long aFirstWeight = ( (Truck)pFirst ).weight; long aSecondWeight = ( (Truck)pSecond ).weight; /* need signum to convert long to int, (int)will not do! */ return signum( aFirstWeight - aSecondWeight ); } // end compare /** * Collapse number down to +1 0 or -1 depending on sign. * Typically used in compare routines to collapse a difference * of two longs to an int. * * @param diff usually represents the difference of two long. * * @return signum of diff, +1, 0 or -1. */ public static final int signum ( long diff ) { if ( diff > 0 ) return 1; if ( diff < 0 ) return -1; else return 0; } // end signum
} // end class ByWeight |
4:以下程序演示了如何对自定义对象进行排序:
package mypack;
import java.util.Arrays; import java.util.Comparator;
public class ComparatorTest {
@SuppressWarnings("unchecked") public static void main(String[] args) { Dog o1 = new Dog("dog1", 1, 5); Dog o2 = new Dog("dog2", 2, 4); Dog o3 = new Dog("dog3", 3, 3); Dog o4 = new Dog("dog4", 4, 2); Dog o5 = new Dog("dog5", 5, 1);
Dog[] dogs = new Dog[] { o1, o4, o3, o5, o2 };
System.out.println("未排序前"); for (int i = 0; i < dogs.length; i++) { Dog dog = dogs[i]; System.out.println(dog.getName()); }
Arrays.sort(dogs, new ByHeightComparator()); System.out.println("使用高度排序之后:"); for (int i = 0; i < dogs.length; i++) { Dog dog = dogs[i]; System.out.println(dog.getName()); }
Arrays.sort(dogs, new ByWeightComparator()); System.out.println("使用重量排序之后:"); for (int i = 0; i < dogs.length; i++) { Dog dog = dogs[i]; System.out.println(dog.getName()); }
} }
class Dog {
private String name;
private int weight;
private int height;
public Dog(String name, int weight, int height) { this.setName(name); this.weight = weight; this.height = height; }
public int getHeight() { return height; }
public void setHeight(int height) { this.height = height; }
public int getWeight() { return weight; }
public void setWeight(int weight) { this.weight = weight; }
public void setName(String name) { this.name = name; }
public String getName() { return name; } }
class ByWeightComparator implements Comparator {
public int compare(Object firstDog, Object secondDog) { int firstWeight = ((Dog) firstDog).getWeight(); int secondWeight = ((Dog) secondDog).getWeight(); return signum(firstWeight - secondWeight); }
public static final int signum(int diff) { if (diff > 0) return 1; if (diff < 0) return -1; else return 0; }
}
class ByHeightComparator implements Comparator {
public int compare(Object firstDog, Object secondDog) { int firstHeight = ((Dog) firstDog).getHeight(); int secondHeight = ((Dog) secondDog).getHeight(); return signum(firstHeight - secondHeight); }
public static final int signum(int diff) { if (diff > 0) return 1; if (diff < 0) return -1; else return 0; }
} |
5:以下程序演示了如何对中文进行排序
package mypack;
import java.text.Collator; import java.text.RuleBasedCollator; import java.util.Arrays; import java.util.Locale;
public class Test { public static void main(String[] args) { String[] test = new String[] { "的", "波", "次", "啊"}; Arrays.sort(test, (RuleBasedCollator) Collator.getInstance(Locale.CHINA)); for (String key : test) System.out.println(key); }
} |
详细信息参见: http://www.javaresearch.org/article/53609.htm