针对集合中的某个字段对集合中的对象进行排序

class MappSentence {
private String sentence;//匹配到的词
private int length;//匹配到的词的长度
private int mappedLength;//与原短语相同单词的长度
private String standardWord;//映射到的规范词
private double score;

public MappSentence(double score){

this.score=score;

}
public double getScore() {
 return score;
}
public void setScore(double score) {
 this.score = score;
}
public int getLength() {
 return length;
}
public String getSentence() {
 return sentence;
}
public void setSentence(String sentence) {
 this.sentence = sentence;
}
public String getStandardWord() {
 return standardWord;
}
public void setStandardWord(String standardWord) {
 this.standardWord = standardWord;
}
public void setLength(int length) {
 this.length = length;
}
public int getMappedLength() {
 return mappedLength;
}
public void setMappedLength(int mappedLength) {
 this.mappedLength = mappedLength;
}
}

 

class ComparatorScore implements Comparator<MappSentence>{
 @Override
 public int compare(MappSentence m1, MappSentence m2) {
    int flag=-1;
    if(m1.getScore()>m2.getScore()){
     flag=1;
    }
    if(m1.getScore()==m2.getScore()){
     flag=0;
    }
    return flag;
 }
}

 

public class TestSort {

 /**
  * @param args
  */
 public static void main(String[] args)
 {
  List mps=new ArrayList();
  mps.add(new MappSentence(100));
  mps.add(new MappSentence(12));
  mps.add(new MappSentence(31));
  mps.add(new MappSentence(14));
  mps.add(new MappSentence(5));
  mps.add(new MappSentence(16));
  mps.add(new MappSentence(70));
  ComparatorScore cm=new ComparatorScore();
   Collections.sort(mps,cm); 
   for(int i=0;i<mps.size();i++){
    MappSentence mapp=(MappSentence)mps.get(i);
    System.out.println(mapp.getScore());
   }
 }
}

测试结果:

 

5.0
12.0
14.0
16.0
31.0
70.0
100.0

转载于:https://www.cnblogs.com/hxy520/archive/2013/02/28/2937128.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值