简体   繁体   English

基于对象属性对对象列表排序

[英]Sort Array list of objects based on object attributes

I have list which contains a property class object, In the list i have 3 status 我有包含属性类对象的列表,在列表中我有3个状态

  • not_paid 未支付
  • paid 支付
  • part_paid part_paid

I want to sort my list below mentioned order. 我想按下面提到的顺序对我的列表进行排序。

First - not_paid second- part_paid third -paid 首先 - not_paid second-part_paid third -paid

How can I sort my list using Comparator class.? 如何使用Comparator类对列表进行排序。

public static Comparator<OrderHistoryItemData> COMPARE_BY_PAYMENT = new Comparator<OrderHistoryItemData>() {
    public int compare(OrderHistoryItemData one, OrderHistoryItemData other) {
        String p1 = one.getAttributes().getFieldPaymentStatus();
        String p2 = other.getAttributes().getFieldPaymentStatus();
        if (p1.equals(p2)) {
            return 0;
        }
        if (p1.equals("not_paid") && (p2.equals("part_paid") || p2.equals("not_paid"))) {
            return -1;
        }
        if (p1.equals("not_paid") && p2.equals("not_paid")) {
            return -1;
        }
        return 1;
    }
};

This is my Code. 这是我的代码。 i am getting below order using this code. 我使用此代码获得低于订单。

paid-->not_paid-->part_paid 支付 - > not_paid - > part_paid

This is my Update Code. 这是我的更新代码。 I got my result. 我得到了我的结果。

public static Comparator<OrderHistoryItemData> COMPARE_BY_PAYMENT = new Comparator<OrderHistoryItemData>() {
    public int compare(OrderHistoryItemData one, OrderHistoryItemData other) {
        String p1 = one.getAttributes().getFieldPaymentStatus();
        String p2 = other.getAttributes().getFieldPaymentStatus();
        if (p1.equals(p2)) {
            return 0;
        }
        if (p1.equals("not_paid") && (p2.equals("part_paid") || p2.equals("paid"))) {
            return -1;
        }
        if (p1.equals("part_paid") && p2.equals("paid")) {
            return -1;
        }
        return 1;
    }
};

To avoid complex comparator, I encourage you to export your statuses to an enum . 为避免复杂的比较,我鼓励您将状态导出到enum (Plus this will work if you will add more statuses in the future, without the need to change logic in your comparator): (另外,如果您将来添加更多状态,而不需要更改比较器中的逻辑),这将起作用:

enum PaymentStatus { // Write them in order you want to be sorted
    NOT_PAID,
    PART_PAID,
    PAID
}

Then sorting will be as simple as : 然后排序将如下所示:

list.sort(Comparator.comparing(item ->item.getAttributes().getFieldPaymentStatus())); 

What you can do is first mapping the strings to integers in the desired order, and then simply subtracting them from eachother. 你可以做的是首先按所需的顺序将字符串映射到整数,然后简单地从彼此中减去它们。

private static Comparator<Payments> comparator = new Comparator<Payments>() {

    // Use this mapping function to map the statuses to ints.
    // The lowest number comes first
    private int map(String str) {
        switch (str) {
            case "not_paid":
                return 0;
            case "part_paid":
                return 1;
            case "paid":
                return 2;
            default:
                return 3;
        }
    }
    // Alternatively, you can use the Map interface to define the sorting
    // order.

    @Override
    public int compare(Payments o1, Payments o2) {
        return map(o1.status) - map(o2.status);
    }
};

I suggest – Schidu Luca already mentioned it in his answer – that you use enum s to define a fixed set of known values, like payment statuses. 我建议 - Schidu Luca 在他的回答中已经提到 - 你使用enum来定义一组固定的已知值,比如支付状态。 This provides compile-time safety. 这提供了编译时的安全性。

Note: I wouldn't, however, suggest to bind the enum declaration order to the sorting order. 注意:但是,我不建议将枚举声明顺序绑定到排序顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM