Vincent

Vicent's blog
随笔 - 74, 文章 - 0, 评论 - 5, 引用 - 0
数据加载中……

java中对象的排序

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()
 {

 }

}

posted on 2006-08-24 15:21 Binary 阅读(336) 评论(0)  编辑  收藏 所属分类: j2se


只有注册用户登录后才能发表评论。


网站导航: