java arraylist 对象排序,在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 sortedListByEmail(ArrayList Billing) {

ArrayList Sort = new ArrayList();

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

Collections.sort(Sort, new Comparator() {

public int compare(Billing o1, Billing o2) {

return o1.getEmail() > o2.getEmail() ? -1 : o1.getEmail().equals(o2.getEmail() ? 0 : 1);

}

});

}

return Sort;

}

Rest of the class:

import java.util.ArrayList;

import java.util.Collections;

import java.lang.Comparable;

import java.util.Comparator;

public class Billing extends User implements Comparable {

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

解决方案

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 sortedListByEmail(List Billing) {

List sort = new ArrayList<>(Billing);

Collections.sort(sort, new Comparator() {

public int compare(Billing o1, Billing o2) {

return o1.getEmail().compareTo(o2.getEmail());

}

}

);

return sort;

}

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

public static List sortedListByEmail(List Billing) {

List 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.

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 sortedListByEmail(List Billing) {

return Billing.stream()

.sorted(Comparator.comparing(b -> b.getEmail()))

.collect(Collectors.toList());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值