นี่คือบทแนะนำเกี่ยวกับการสั่งซื้อวัตถุ:
ถึงแม้จะยกตัวอย่างไปบ้าง แต่ก็อยากแนะนำให้อ่านอยู่ดี
มีหลายวิธีในการจัดเรียงArrayListไฟล์. หากคุณต้องการที่จะกำหนดธรรมชาติ (เริ่มต้น) การสั่งซื้อแล้วคุณต้องปล่อยให้ดำเนินการContact Comparableสมมติว่าคุณต้องการจัดเรียงตามค่าเริ่มต้นnameจากนั้นทำ (ละเว้น nullcheck เพื่อความเรียบง่าย):
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);
หากคุณต้องการกำหนดคำสั่งที่ควบคุมได้ภายนอก (ซึ่งจะแทนที่ลำดับธรรมชาติ) คุณจะต้องสร้าง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());
    }
}); 
คุณสามารถกำหนดComparators ในContactตัวเองเพื่อให้คุณสามารถใช้ซ้ำได้แทนที่จะสร้างขึ้นใหม่ทุกครั้ง:
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));
    }
}
ซึ่งคุณสามารถใช้ได้ดังต่อไปนี้:
// Sort on "phone" field of the Contact bean.
Collections.sort(contacts, new BeanComparator("phone"));
(ตามที่คุณเห็นในโค้ดอาจมีการครอบคลุมช่องว่างเพื่อหลีกเลี่ยง NPE ระหว่างการเรียงลำดับ)