给你个例子看了应该会明白的
要使对象可排序,应该实现Comparable
并复写里面的compareTo函数
放到容器里就可以调用sort()排序了
此例按priority排序/*进程块的描述,是可比较的*/
class MyProcess implements Comparable{private int priority;
private int id;
private int cpuTime;
private int allTime;
private int startBlock;
private int blockTime;
private boolean state;
private int tempBlockTime;public MyProcess(int priority,int id,int cpuTime,int allTime,int startBlock,
int blockTime,boolean state){
this.priority=priority;
this.id=id;
this.cpuTime=cpuTime;
this.allTime=allTime;
this.startBlock=startBlock;
this.blockTime=blockTime;
this.state=state;
}public void setPriority(int priority){
this.priority=priority;
}public int getPriority(){
return priority;
}public void setID(int id){
this.id=id;
}public int getID(){
return id;
}public void setCpuTime(int cpuTime){
this.cpuTime=cpuTime;
}public int getCpuTime(){
return cpuTime;
}public void setAllTime(int allTime){
this.allTime=allTime;
}public int getAllTime(){
return allTime;
}public void setStartBlock(int startBlock){
this.startBlock=startBlock;
}public int getStartBlock(){
return startBlock;
}public void setBlockTime(int blockTime){
this.blockTime=blockTime;
}public int getBlockTime(){
return blockTime;
}public void setState(boolean state){
this.state=state;
}public String getState(){
if(state==true){
return "READY";
}
return "BLOCK";
}public String toString(){
return (""+getID()+"        "+getPriority()+"          "+getCpuTime()+"          "+
           getAllTime()+"            "+getStartBlock()+"           "+getBlockTime()+"        "+getState());
}public int compareTo(Object v){
MyProcess other  = (MyProcess)v;
   int i = other.getPriority();
   return priority>i?1:(priority==i?0:-1);
}
/**
 * @return
 */
public int getTempBlockTime() {
return tempBlockTime;
}/**
 * @param 
 */
public void setTempBlockTime(){
this.tempBlockTime = this.blockTime;
}public void setTempBlockTime2(){
this.tempBlockTime=this.tempBlockTime-1;
}}