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 閱讀(344) 評論(0)  編輯  收藏 所屬分類: j2se

          主站蜘蛛池模板: 卢湾区| 昔阳县| 大悟县| 行唐县| 连山| 北辰区| 阜新| 南城县| 西贡区| 苏州市| 平远县| 临夏市| 温州市| 海口市| 和田市| 沭阳县| 永安市| 江山市| 来安县| 沁水县| 鄂托克旗| 开化县| 营口市| 句容市| 太谷县| 麻阳| 湄潭县| 宁德市| 平谷区| 叙永县| 遂川县| 吉木乃县| 江华| 潜江市| 襄垣县| 宁陵县| 中西区| 福清市| 威信县| 浪卡子县| 明星|