這是代碼。。。。java
package com.bjsasc.avplan.web.handler.test.objectSort;web
import com.bjsasc.plm.core.foundation.Helper;ide
public class Duck implements Comparable<Object>{
String name;
int weight;
public Duck (String name,int weight){
this.name = name;
this.weight = weight;
}
public String toString(){
return name + " weights " + weight;
}
@Override
public int compareTo(Object o) {
Duck otherDuck = (Duck)o;
/* int lastCmp = (this.weight).compareTo(otherDuck.weight);
// 首先比較姓(lastName)若是姓相同(lastCmp==0)再比較名(firstName),不然返回名的比較
return (lastCmp == 0 ? (this.name).compareTo(otherDuck.name) : lastCmp); */
if(this.weight<otherDuck.weight ){
return -1;
}else if(this.weight==otherDuck.weight ){
return 0;
}else{
return 1;
}
}this
}.net
===================================================get
package com.bjsasc.avplan.web.handler.test.objectSort;io
import java.util.Arrays;ast
import com.oscar.jdbc.Array;class
import jmathlib.toolbox.jmathlib.system.foreach;test
public class DuckSort {
/**
* @param args
*/
public static void main(String[] args) {
Duck [] ducks = {
new Duck("aaaa",8),
new Duck("bbb",2),
new Duck("eee",7),
new Duck("fff",2),
new Duck("hhh",3),
new Duck("ttt",10)
};
System.out.println("before sort:");
display(ducks);
System.out.println("\nafter sort:");
Arrays.sort(ducks);
display(ducks);
}
public static void display(Duck [] ducks){
for(Duck duck:ducks){
System.out.println(duck);
}
}
}