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

 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值