排序的基础上的名字联系的ArrayList? [英] Sorting an ArrayList of Contacts based on name?

查看:114
本文介绍了排序的基础上的名字联系的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  排序对象的集合

好了,所以我有一个已经作出通讯录应用程序并pretty多完成了所有的主要功能,但我期待在执行程序中的排序功能。

Ok so I have a been making an addressbook application and have pretty much finished all the key features but I am looking to implement a sort feature in the program.

我要排序的ArrayList是一个叫做联系(contactArray)类型,它是一个包含四个字段单独的类的;姓名,家庭号码,手机号码和地址。
所以我一直在寻找到使用集合排序没有我不知道我怎么会实现这一点。

I want to sort an Arraylist which is of a type called Contact (contactArray) which is a separate class which contains four fields; name, home number, mobile number and address. So I was looking into using the collection sort yet am not sure how i'd implement this.

这是我应该使用合适的排序/是否有可能使用或我应该考虑做一个自定义排序?

Is this the right sort I should be using / is it possible to use or should I look into making a custom sort?

推荐答案

下面是一个关于订购的对象教程:

Here's a tutorial about ordering objects:

  • The Java Tutorials - Collections - Object Ordering

虽然我会举一些例子,我建议无论如何读它。

Although I will give some examples, I would recommend to read it anyway.

有多种方式排序的的ArrayList 。如果要定义的自然(默认)排序,然后你需要让联系实施<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html\"><$c$c>Comparable.假设你希望默认排序名称,然后做(为了简化,省略nullchecks):

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;

    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);


如果您想定义的外部可控排序(这将覆盖自然排序),那么你需要创建一个<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html\"><$c$c>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());
    }
}); 


您甚至可以定义比较■在联系本身,这样就可以代替重用重建他们的他们每次:


You can even define the Comparators 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"));

(如您在code看到,可能是空字段已覆盖以避免排序中NPE的)

这篇关于排序的基础上的名字联系的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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