使用自定义排序顺序对对象的 ArrayList 进行排序 [英] Sorting an ArrayList of objects using a custom sorting order
问题描述
我希望为我的地址簿应用程序实现排序功能.
I am looking to implement a sort feature for my address book application.
我想对 ArrayList
.Contact
是一个包含四个字段的类:姓名、家庭号码、手机号码和地址.我想按 name
排序.
I want to sort an ArrayList<Contact> contactArray
. Contact
is a class which contains four fields: name, home number, mobile number and address. I want to sort on name
.
如何编写自定义排序函数来执行此操作?
How can I write a custom sort function to do this?
推荐答案
这是一个关于排序对象的教程:
Here's a tutorial about ordering objects:
虽然我会举一些例子,但我还是建议你阅读它.
Although I will give some examples, I would recommend to read it anyway.
有多种方法可以对 ArrayList
进行排序.如果你想定义一个自然(默认)排序,那么你需要让Contact
实现Comparable
.假设您希望默认对 name
进行排序,然后执行(为简单起见省略了空检查):
There are various way to sort an ArrayList
. If you want to define a natural (default) ordering, then you need to let Contact
implement Comparable
. Assuming that you want to sort by default on name
, then do (nullchecks omitted for simplicity):
public class Contact implements Comparable<Contact> {
private String name;
private String phone;
private Address address;
@Override
public int compareTo(Contact other) {
return name.compareTo(other.name);
}
// Add/generate getters/setters and other boilerplate.
}
这样你就可以做
List<Contact> contacts = new ArrayList<Contact>();
// Fill it.
Collections.sort(contacts);
如果要定义外部可控排序(覆盖自然排序),则需要创建一个Comparator
:
If you want to define an external controllable ordering (which overrides the natural ordering), then you need to create a Comparator
:
List<Contact> contacts = new ArrayList<Contact>();
// Fill it.
// Now sort by address instead of name (default).
Collections.sort(contacts, new Comparator<Contact>() {
public int compare(Contact one, Contact other) {
return one.getAddress().compareTo(other.getAddress());
}
});
您甚至可以在 Contact
本身中定义 Comparator
以便您可以重用它们而不是每次都重新创建它们:
You can even define the Comparator
s in the Contact
itself so that you can reuse them instead of recreating them everytime:
public class Contact {
private String name;
private String phone;
private Address address;
// ...
public static Comparator<Contact> COMPARE_BY_PHONE = new Comparator<Contact>() {
public int compare(Contact one, Contact other) {
return one.phone.compareTo(other.phone);
}
};
public static Comparator<Contact> COMPARE_BY_ADDRESS = new Comparator<Contact>() {
public int compare(Contact one, Contact other) {
return one.address.compareTo(other.address);
}
};
}
可以如下使用:
List<Contact> contacts = new ArrayList<Contact>();
// Fill it.
// Sort by address.
Collections.sort(contacts, Contact.COMPARE_BY_ADDRESS);
// Sort later by phone.
Collections.sort(contacts, Contact.COMPARE_BY_PHONE);
为了使顶部更加完美,您可以考虑使用通用 javabean 比较器:
public class BeanComparator implements Comparator<Object> {
private String getter;
public BeanComparator(String field) {
this.getter = "get" + field.substring(0, 1).toUpperCase() + field.substring(1);
}
public int compare(Object o1, Object o2) {
try {
if (o1 != null && o2 != null) {
o1 = o1.getClass().getMethod(getter, new Class[0]).invoke(o1, new Object[0]);
o2 = o2.getClass().getMethod(getter, new Class[0]).invoke(o2, new Object[0]);
}
} catch (Exception e) {
// If this exception occurs, then it is usually a fault of the developer.
throw new RuntimeException("Cannot compare " + o1 + " with " + o2 + " on " + getter, e);
}
return (o1 == null) ? -1 : ((o2 == null) ? 1 : ((Comparable<Object>) o1).compareTo(o2));
}
}
您可以按如下方式使用:
which you can use as follows:
// Sort on "phone" field of the Contact bean.
Collections.sort(contacts, new BeanComparator("phone"));
(如您在代码中看到的,可能已经覆盖了空字段以避免排序期间出现 NPE)
这篇关于使用自定义排序顺序对对象的 ArrayList 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!