在项目中,讲资源按发表时间顺序来排列,最先发表的放在最前面。资源位于三个不同的分项目,用到了webService调用
ResourceXmlRpcClient rpc =new ResourceXmlRpcClient();
Object[] listBar=rpc.getNewBarTopic(barId);
Object[] listProject=rpc.getSdStudioProjectList(tbId);
list.addAll(Arrays.asList(listProject));
list.addAll(Arrays.asList(listBar));
Collections.sort(list, new Comparator<HashMap<String,Object>>() {
@Override
public int compare(HashMap<String,Object> row0, HashMap<String,Object> row1) {
long t1 = ((Date)row0.get("CRE_TIME")).getTime();
long t2 = ((Date)row1.get("CRE_TIME")).getTime();
return new Long(t2-t1).intValue();
}
});
这里需要引入jar backport-util-concurrent-3.0.jar
自己的测试
public List orderBySort(){
List list=new ArrayList();
list.add(1);
list.add(5);
list.add(2);
list.add(3);
list.add(6);
list.add(4);
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i));//152364
}
//list中的对象A实现Comparable接口
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
System.out.print("第二次"+list.get(i));//
}
List<A> listA = new ArrayList<A>();
List<B> listB = new ArrayList<B>();
List<B> listC = new ArrayList<B>();
A aa = new A();
aa.setName("aa");
aa.setOrder(3);
A bb = new A();
bb.setName("bb");
bb.setOrder(2);
listA.add(bb);
listA.add(aa);
//list中的对象A实现Comparable接口 从小到大排列
Collections.sort(listA);
System.out.println(listA);
B ab = new B();
ab.setName("ab");
ab.setOrder("4");
B ba = new B();
ba.setName("ba");
ba.setOrder("2");
listB.add(ba);
listB.add(ab);
//根据Collections.sort重载方法来实现
Collections.sort(listB,new Comparator<B>(){
@Override
public int compare(B b1, B b2) {
return b1.getOrder().compareTo(b2.getOrder()); //从小到大
}
});
System.out.println(listB);
B ab1 = new B();
ab1.setName("ab");
ab1.setOrder("4");
B ba1 = new B();
ba1.setName("ba");
ba1.setOrder("2");
listC.add(ba1);
listC.add(ab1);
//根据Collections.sort重载方法来实现
Collections.sort(listB,new Comparator<B>(){
@Override
public int compare(B b1, B b2) {//转换的是string
int t1=(Integer.valueOf(b1.getOrder()));
int t2=(Integer.valueOf(b2.getOrder()));
return t2-t1; //从小到大
}
});
System.out.println(listC);
return list;
}
其中
A
package bean;
public class A implements Comparable<A>{
private String name;
private Integer order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
@Override
public String toString() {
return "name is "+name+" order is "+order;
}
@Override
public int compareTo(A a) {
return this.order.compareTo(a.getOrder());
}
B
package bean;
public class B {
private String name;
private String order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
@Override
public String toString() {
return "name is "+name+" order is "+order;
}
}
import edu.emory.mathcs.backport.java.util.Collections;
import java.util.Calendar;