本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7591627
建立时间:2006年5月29日




[J2SE]java中对象的排序
软件技术,  电脑与网络

lhwork 发表于 2006/6/27 13:24:05

1:排序类package com.tixa.bad.customer.util; import java.util.ArrayList;import java.util.Collections;import java.util.Comparator; import com.tixa.bad.customer.data.AdstatGraphArea; /** * 对地区数据进行排序 *  * @StatAreaSort *  * @author  *  * TODO */public class StatAreaSort{ /**  * 点击排序规则,倒序  */ private static Comparator orderClick = new Comparator() {  public int compare(Object o1, Object o2)  {   AdstatGraphArea b1 = (AdstatGraphArea) o1;   AdstatGraphArea b2 = (AdstatGraphArea) o2;   return (b2.getClickcount() - b1.getClickcount());  } };  /**  * ip排序规则,倒序  */ private static Comparator orderIP = new Comparator() {  public int compare(Object o1, Object o2)  {   AdstatGraphArea b1 = (AdstatGraphArea) o1;   AdstatGraphArea b2 = (AdstatGraphArea) o2;   return (b2.getIpcount() - b1.getIpcount());  } };  /**  * 根据点击量进行排序  *   * @param list  */ public static void getSortClick(ArrayList list) {  Collections.sort(list, orderClick);  // Collections.reverse(list); }  /**  * 根据ip量进行排序  *   * @param list  */ public static void getSortIp(ArrayList list) {  Collections.sort(list, orderIP);  // Collections.reverse(list); } }2:对排序进行测试package com.tixa.bad.customer.util; import java.util.ArrayList; import junit.framework.TestCase; import com.tixa.bad.customer.data.AdstatGraphArea; public class StatAreaSortTest extends TestCase{  protected void setUp() throws Exception {  super.setUp(); }  protected void tearDown() throws Exception {  super.tearDown(); }  /*  * Test method for 'com.tixa.bad.customer.util.StatAreaSort.getSortClick(ArrayList)'  */ public void testGetSortClick() {    ArrayList list = new ArrayList();      AdstatGraphArea graAre = new AdstatGraphArea();   graAre.setAdid(1);   graAre.setClickcount(786);   graAre.setIpcount(43453);   list.add(graAre);      AdstatGraphArea graAre1 = new AdstatGraphArea();   graAre1.setAdid(2);   graAre1.setClickcount(987876);   graAre1.setIpcount(545);   list.add(graAre1);      AdstatGraphArea graAre2 = new AdstatGraphArea();   graAre2.setAdid(3);   graAre2.setClickcount(877887);   graAre2.setIpcount(4534534);   list.add(graAre2);      AdstatGraphArea graAre3 = new AdstatGraphArea();   graAre3.setAdid(4);   graAre3.setClickcount(97998);   graAre3.setIpcount(34534);   list.add(graAre3);      AdstatGraphArea graAre4 = new AdstatGraphArea();   graAre4.setAdid(5);   graAre4.setClickcount(500);   graAre4.setIpcount(2000);   list.add(graAre4);      System.out.print("sore before ");   for(int i = 0;i<list.size();i++)   {    AdstatGraphArea a = (AdstatGraphArea)list.get(i);    System.out.println(a.getAdid());   }      StatAreaSort.getSortClick(list);      System.out.print("sore after");   for(int i = 0;i<list.size();i++)   {    AdstatGraphArea a = (AdstatGraphArea)list.get(i);    System.out.println(a.getAdid());   }   System.out.println("----------------------");      StatAreaSort.getSortIp(list);   for(int i = 0;i<list.size();i++)   {    AdstatGraphArea a = (AdstatGraphArea)list.get(i);    System.out.println(a.getAdid());   } }  /*  * Test method for 'com.tixa.bad.customer.util.StatAreaSort.getSortIp(ArrayList)'  */ public void testGetSortIp() {  } }


阅读全文(1760) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.383 second(s), page refreshed 144778442 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号