在Java中按对象的属性对ArrayList进行排序 [英] Sort an ArrayList by an object's attribute in java

查看:180
本文介绍了在Java中按对象的属性对ArrayList进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望按照对象的电子邮件地址对其进行排序.

I wish to sort my objects in order of their Email address.

这是我尝试过的方法,但是它不起作用,但是我什至不确定这是否是我想要做的正确方法?

This is the method I've attempted but it does not work, but I'm not even sure it's the correct way to do what I want?

public static ArrayList<Billing> sortedListByEmail(ArrayList<Billing> Billing) {
    ArrayList<Billing> Sort = new ArrayList<Billing>();

    for (int i = 0; i < Sort.size(); i++) {
        Collections.sort(Sort, new Comparator<Billing>() {
            public int compare(Billing o1, Billing o2) {
                return o1.getEmail() > o2.getEmail() ? -1 : o1.getEmail().equals(o2.getEmail() ? 0 : 1);
            }
        });
    }

    return Sort;
}

班级休息时间:

import java.util.ArrayList;
import java.util.Collections;
import java.lang.Comparable;
import java.util.Comparator;

public class Billing extends User implements Comparable<User> {
    private Address billingAddress;
    private String email;

    public Billing(String id, String firstName, String lastName, String userName, String password, UserType userType, PermissionType permission, Boolean Status, Address billingAddress, String email) {
        super(id, firstName, lastName, userName, password, userType, permission, Status);

        this.billingAddress = billingAddress;
        this.email = email;
    }

    public Billing(String id, String firstName, String lastName, String userName, String password, UserType userType, PermissionType permission, Boolean Status, String email) {
        super(id, firstName, lastName, userName, password, userType, permission, Status);

    }

    public Billing(String id, String firstName, String lastName, String userName, String password, UserType userType, PermissionType permission, Boolean Status) {
        super(id, firstName, lastName, userName, password, userType, permission, Status);
    }

    public Address getBillingAddress() {
        return billingAddress;
    }

    public void setBillingAddress(Address billingAddress) {
        this.billingAddress = billingAddress;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

我将如何按照对象的电子邮件地址对对象进行排序?谢谢

How would I accomplish sorting the objects in order of their email address? Thank you

推荐答案

由于emailString也是Comparable,因此可以使用String.compareTo使用email属性进行排序. (这是Java-8之前的解决方案):

Since the email is a String and also a Comparable you can use the String.compareTo to sort using the email property. (This is a pre Java-8 solution):

public static List<Billing> sortedListByEmail(List<Billing> Billing) {
    List<Billing> sort = new ArrayList<>(Billing);
    Collections.sort(sort, new Comparator<Billing>() {
            public int compare(Billing o1, Billing o2) {
                   return o1.getEmail().compareTo(o2.getEmail());
            }
      }
   );
   return sort;
}

在Java-8中,可以使用Comparator#comparing使其更加紧凑:

In Java-8 this can be made more compact, with the Comparator#comparing:

public static List<Billing> sortedListByEmail(List<Billing> Billing) {
        List<Billing> sorted = new ArrayList<>(Billing);
        sorted.sort(Comparator.comparing(b -> b.getEmail()));
        return sorted;
}

此处提供的原始列表未在适当位置进行排序,而是制作了一份已排序的副本.

Here the original list supplied is not sorted in place, instead a copy is made which is sorted.

或者您可以使用Stream#sorted以一种优雅的方式进行操作,这也将返回一个新的List而不是修改原始的List:

Or you can use Stream#sorted to do it in an elegant way which will also return you a new List instead of modifying the original one:

public static List<Billing> sortedListByEmail(List<Billing> Billing) {
    return Billing.stream()
                  .sorted(Comparator.comparing(b -> b.getEmail()))
                  .collect(Collectors.toList());
}

这篇关于在Java中按对象的属性对ArrayList进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆