利用compareTo对java基于类的List进行排序
大致逻辑如下:先让类继承接口Comparable,并按照业务逻辑重写compareTo方法,然后调用Collections.sort(List)来进行排序。
分2个类:CompaerClass和CopmpareClassTest,在CompaerClass中继承接口Comparable,重写compareTo方法(增加按照4个字段进行排序的逻辑),在CopmpareClassTest生成测试数据,并进行排序,并输出结果。
一:CompaerClass类
public class CompaerClass implements Comparable {
private int a;
private String b;
private String c;
private String d;
private String e;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public String getD() {
return d;
}
public void setD(String d) {
this.d = d;
}
public String getE() {
return e;
}
public void setE(String e) {
this.e = e;
}
public int compareTo(Object tt) {
CompaerClass t = (CompaerClass) tt;
if((t.getA()+t.getB()+t.getC()+t.getD()).compareTo(this.a+this.b+this.c+this.d)>0)
return -1;
else if((t.getA()+t.getB()+t.getC()+t.getD()).compareTo(this.a+this.b+this.c+this.d)==0)
return 0;
else return 1;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + a;
result = prime * result + ((b == null) ? 0 : b.hashCode());
result = prime * result + ((c == null) ? 0 : c.hashCode());
result = prime * result + ((d == null) ? 0 : d.hashCode());
result = prime * result + ((e == null) ? 0 : e.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof CompaerClass))
return false;
final CompaerClass ther = (CompaerClass) obj;
if (a != other.a)
return false;
if (b == null) {
if (other.b != null)
return false;
} else if (!b.equals(other.b))
return false;
if (c == null) {
if (other.c != null)
return false;
} else if (!c.equals(other.c))
return false;
if (d == null) {
if (other.d != null)
return false;
} else if (!d.equals(other.d))
return false;
if (e == null) {
if (other.e != null)
return false;
} else if (!e.equals(other.e))
return false;
return true;
}
}
二:CopmpareClassTest类
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class CopmpareClassTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List t = new ArrayList();
CompaerClass c = new CompaerClass();
c.setA(1);
c.setB("a");
c.setC("h");
c.setD("o");
c.setE("u");
t.add(c);
c = new CompaerClass();
c.setA(5);
c.setB("d");
c.setC("i");
c.setD("q");
c.setE("x");
t.add(c);
c = new CompaerClass();
c.setA(4);
c.setB("g");
c.setC("l");
c.setD("w");
c.setE("u");
t.add(c);
c = new CompaerClass();
c.setA(9);
c.setB("b");
c.setC("n");
c.setD("u");
c.setE("y");
t.add(c);
c = new CompaerClass();
c.setA(5);
c.setB("d");
c.setC("i");
c.setD("p");
c.setE("x");
t.add(c);
printTest(t);
System.out.println("sort up");
Collections.sort(t);
printTest(t);
}
private static void printTest(List q) {
for (Iterator t = q.iterator(); t.hasNext();) {
CompaerClass com = (CompaerClass) t.next();
System.out.println(com.getA() + "|" + com.getB() + "|" + com.getC()
+ "|" + com.getD() + "|" + com.getE());
}
}
}
测试结果如下:
1|a|h|o|u
5|d|i|q|x
4|g|l|w|u
9|b|n|u|y
5|d|i|p|x
sort up
1|a|h|o|u
4|g|l|w|u
5|d|i|p|x
5|d|i|q|x
9|b|n|u|y
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/694276/viewspace-557817/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/694276/viewspace-557817/