1、实例类
package sh.testsort;
import java.math.BigDecimal;
import java.util.Comparator;
public class BAEntity implements Comparable {
private int testNo;
public int getTestNo() {
return testNo;
}
public void setTestNo(int testNo) {
this.testNo = testNo;
}
private String BANo;
private String ClaimNo;
private BigDecimal ClaimMoney;
public String getBANo() {
return BANo;
}
public void setBANo(String bANo) {
BANo = bANo;
}
public String getClaimNo() {
return ClaimNo;
}
public void setClaimNo(String claimNo) {
ClaimNo = claimNo;
}
public BigDecimal getClaimMoney() {
return ClaimMoney;
}
public void setClaimMoney(BigDecimal claimMoney) {
ClaimMoney = claimMoney;
}
public int compareTo(Object o) {
BAEntity bae=(BAEntity)o;
return BANo.compareTo(bae.getBANo());
}
}
2、main函数
package sh.testsort;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestSort {
private List<BAEntity> testList=null;
private BAEntity baentity=null;
/**
* 对一个LIST进行排序
*/
public void sortList(){
testList=new ArrayList<BAEntity>();
//add first ele
baentity=new BAEntity();
baentity.setTestNo(2);
baentity.setBANo("4280003002011000363");
baentity.setClaimNo("8281103382011000321");
baentity.setClaimMoney(new BigDecimal(100));
testList.add(baentity);
//add second ele
baentity=new BAEntity();
baentity.setTestNo(3);
baentity.setBANo("4280003002011000359");
baentity.setClaimNo("8281103382011000317");
baentity.setClaimMoney(new BigDecimal(200));
testList.add(baentity);
//add third ele
baentity=new BAEntity();
baentity.setTestNo(1);
baentity.setBANo("4280003002011000357");
baentity.setClaimNo("8281103382011000316");
baentity.setClaimMoney(new BigDecimal(300));
testList.add(baentity);
//排序前
System.out.println("Before sort order :>>>>");
for( BAEntity baentity : testList ){
System.out.println(baentity.getTestNo());
System.out.println(baentity.getBANo());
System.out.println(baentity.getClaimNo());
System.out.println(baentity.getClaimMoney());
}
Collections.sort(testList);
//排序后
System.out.println("After sort order :>>>>"+testList);
for( BAEntity baentity : testList ){
System.out.println(baentity.getTestNo());
System.out.println(baentity.getBANo());
System.out.println(baentity.getClaimNo());
System.out.println(baentity.getClaimMoney());
}
}
public static void main(String args[]){
TestSort ts=new TestSort();
ts.sortList();
}
}