好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

List集合中对数据实现多重规则进行排序的案例

list集合进行排序时,很多人会考虑冒泡、快速等排序算法,但是对于多重排序规则的话,算法就不太适用了。其实 java.util.collections 已经提供了 sort 的排序方法,并且能自己实现其排序规则。

现在有个场景:我需要对一批优惠券进行排序,优惠券有三个属性:是否可用、券类型、面额。我需要将可用的、券类型最大的、面额最大的券排到最前面。

即优先按是否可用排序,其次是券类型,再者就是面额。    

话不多说,看代码吧:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

package com.test;

import java.math.bigdecimal;

import java.util.arraylist;

import java.util.collections;

import java.util.comparator;

/**

  * list多重规则排序测试类

  */

public class testcompartor {

   public static void main(string[] args) {

     arraylist<coupon> persons = new arraylist<coupon>();

     persons.add( new coupon( 13 , 0 , new bigdecimal( 40 )));

     persons.add( new coupon( 13 , 0 , new bigdecimal( 50 )));

     persons.add( new coupon( 13 , 0 , new bigdecimal( 45 )));

     persons.add( new coupon( 1 , 0 , new bigdecimal( 20 )));

     persons.add( new coupon( 13 , 1 , new bigdecimal( 30 )));

     persons.add( new coupon( 1 , 0 , new bigdecimal( 25 )));

     persons.add( new coupon( 11 , 0 , new bigdecimal( 50 )));

     persons.add( new coupon( 11 , 1 , new bigdecimal( 40 )));

     system.out.println( "排序之前:" );

     for ( int i = 0 ; i <persons.size(); i++) {

       system.out.println(persons.get(i));

     }

     system.out.println();

     collections.sort(persons, new comparator<coupon>() {

       //按可用升序,券类型降序,面额降序

       public int compare(coupon o1, coupon o2) {

         if (o1.valueable.compareto(o2.valueable)== 0 ){

          if (o2.themetype.compareto(o1.themetype)== 0 ){

          return o2.amount.compareto(o1.amount)> 0 ? 1 :- 1 ;

          } else {

          return o2.themetype - o1.themetype;

          }

         } else {

           return o1.valueable-o2.valueable ;

         }

       }

     });

     system.out.println( "排序后结果:" );

     for ( int i = 0 ; i <persons.size(); i++) {

       system.out.println(persons.get(i));

     }

   }

   static class coupon{

     public integer themetype; //优惠券类型

     public integer valueable; //可用 ,0 可用,1不可用

     public bigdecimal amount; //面额

     @override

     public string tostring() {

       return "person{" +

           "themetype=" + themetype +

           ", valueable=" + valueable +

           ", amount=" + amount +

           '}' ;

     }

  public coupon(integer themetype, integer valueable, bigdecimal amount) {

   super ();

   this .themetype = themetype;

   this .valueable = valueable;

   this .amount = amount;

  }

   }

}

至于封装工具类我就懒得弄了,有需要的自己封装吧。

这里如果用了 integer 等封装类型,最好自己也做下非空处理。

排序之前:

person{themetype=13, valueable=0, amount=40} person{themetype=13, valueable=0, amount=50} person{themetype=13, valueable=0, amount=45} person{themetype=1, valueable=0, amount=20} person{themetype=13, valueable=1, amount=30} person{themetype=1, valueable=0, amount=25} person{themetype=11, valueable=0, amount=50} person{themetype=11, valueable=1, amount=40} 

排序后结果:

person{themetype=13, valueable=0, amount=50} person{themetype=13, valueable=0, amount=45} person{themetype=13, valueable=0, amount=40} person{themetype=11, valueable=0, amount=50} person{themetype=1, valueable=0, amount=25} person{themetype=1, valueable=0, amount=20} person{themetype=13, valueable=1, amount=30} person{themetype=11, valueable=1, amount=40}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/moneyshi/article/details/71108140

查看更多关于List集合中对数据实现多重规则进行排序的案例的详细内容...

  阅读:9次