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

查看:27
本文介绍了在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

推荐答案

由于 email 是一个 String 也是一个 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 而不是修改原来的:

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天全站免登陆