Sorting an ArrayList of objects using a custom sorting order
Asked Answered
N

12

131

I am looking to implement a sort feature for my address book application.

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?

Nugget answered 28/11, 2009 at 23:18 Comment(0)
P
278

Here's a tutorial about ordering objects:

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


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

so that you can just do

List<Contact> contacts = new ArrayList<Contact>();
// Fill it.

Collections.sort(contacts);

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

}

which can be used as follows:

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

And to cream the top off, you could consider to use a generic javabean comparator:

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

(as you see in the code, possibly null fields are already covered to avoid NPE's during sort)

Pylos answered 28/11, 2009 at 23:25 Comment(13)
I'd add the possibility of pre-defining several comparators, and then using them by name...Billingsgate
In fact, I just did. Easier than trying to explain myself.Billingsgate
@BalusC: No probs. I can't take credit for the idea, I got it from String.CASE_INSENSITIVE_ORDER and friends but I like it. Makes the resulting code easier to read.Billingsgate
Those Comparator definitions should probably also be static and maybe final too... Or something like that..Billingsgate
Heheh... BeanComparator is like Awesome On A Stick! :-) (I don't remember the exact logic comparison of nulls, but does it want a (o1 == null && o2 == null) ? 0 : at the start of that return line?)Billingsgate
Hey thanks for the reply it was really helpful and has greatly improved my understanding of collections. I tried the first one and it did work on the firsname but I need it in a JList ordered by surname so I had a look at the second and just compared a split string, thanks very much :)Nugget
@Stobor: this sorts nulls to top. They doesn't necessarily need both to be null. @Sameera0: you're welcome.Pylos
made mine static and final, thanks for the help! I'm sorting properties by comparing custom Annotations because object.getDeclaredFields is f*ing the attribute orderKaohsiung
One of the best answers I've ever read. As I was implementing the first half about Comparable, I suddenly thought what if I want to search by more than a single search, and then 5 more lines down, and that was answered too..Jobina
This answer is very outdated.Anvers
Good explanationsGladden
@Pylos I want to implement search in Comparators in the Contact method, ex: search by phone number. How can I implement that? Help will be appreciated.Conservatism
Someone give this man a medal.Steelworks
N
31

In addition to what was already posted by BalusC it may be worth pointing that since Java 8 we can shorten our code and write it like:

Collection.sort(yourList, Comparator.comparing(YourClass::getSomeComparableField));

or since List now have sort method also like

yourList.sort(Comparator.comparing(YourClass::getSomeComparableField));

Explanation:

Since Java 8, functional interfaces (interfaces with only one abstract method - they can have more default or static methods) can be easily implemented using:

Since Comparator<T> has only one abstract method int compare(T o1, T o2) it is functional interface.

So instead of (example from @BalusC answer)

Collections.sort(contacts, new Comparator<Contact>() {
    public int compare(Contact one, Contact other) {
        return one.getAddress().compareTo(other.getAddress());
    }
}); 

we can reduce this code to:

Collections.sort(contacts, (Contact one, Contact other) -> {
     return one.getAddress().compareTo(other.getAddress());
});

We can simplify this (or any) lambda by skipping

  • argument types (Java will infer them based on method signature)
  • or {return ... }

So instead of

(Contact one, Contact other) -> {
     return one.getAddress().compareTo(other.getAddress();
}

we can write

(one, other) -> one.getAddress().compareTo(other.getAddress())

Also now Comparator has static methods like comparing(FunctionToComparableValue) or comparing(FunctionToValue, ValueComparator) which we could use to easily create Comparators which should compare some specific values from objects.

In other words we can rewrite above code as

Collections.sort(contacts, Comparator.comparing(Contact::getAddress)); 
//assuming that Address implements Comparable (provides default order).
Nasya answered 6/5, 2016 at 16:53 Comment(0)
B
8

This page tells you all you need to know about sorting collections, such as ArrayList.

Basically you need to

  • make your Contact class implement the Comparable interface by
    • creating a method public int compareTo(Contact anotherContact) within it.
  • Once you do this, you can just call Collections.sort(myContactList);,
    • where myContactList is ArrayList<Contact> (or any other collection of Contact).

There's another way as well, involving creating a Comparator class, and you can read about that from the linked page as well.

Example:

public class Contact implements Comparable<Contact> {

    ....

    //return -1 for less than, 0 for equals, and 1 for more than
    public compareTo(Contact anotherContact) {
        int result = 0;
        result = getName().compareTo(anotherContact.getName());
        if (result != 0)
        {
            return result;
        }
        result = getNunmber().compareTo(anotherContact.getNumber());
        if (result != 0)
        {
            return result;
        }
        ...
    }
}
Bolshevism answered 28/11, 2009 at 23:35 Comment(0)
H
5

BalusC and bguiz have already given very complete answers on how to use Java's built-in Comparators.

I just want to add that google-collections has an Ordering class which is more "powerful" than the standard Comparators. It might be worth checking out. You can do cool things such as compounding Orderings, reversing them, ordering depending on a function's result for your objects...

Here is a blog post that mentions some of its benefits.

Hobbes answered 29/11, 2009 at 0:18 Comment(1)
Note that google-collections is now a part of Guava (Google's common java libraries), so you might want to depend on Guava (or Guava's collection module) if you want to use the Ordering class.Hobbes
H
4

You need make your Contact classes implement Comparable, and then implement the compareTo(Contact) method. That way, the Collections.sort will be able to sort them for you. Per the page I linked to, compareTo 'returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.'

For example, if you wanted to sort by name (A to Z), your class would look like this:

public class Contact implements Comparable<Contact> {

    private String name;

    // all the other attributes and methods

    public compareTo(Contact other) {
        return this.name.compareTo(other.name);
    }
}
Horologium answered 28/11, 2009 at 23:27 Comment(1)
Worked well with me, thanks! I also used compareToIgnoreCase to ignore case.Viaticum
C
3

By using lambdaj you can sort a collection of your contacts (for example by their name) as it follows

sort(contacts, on(Contact.class).getName());

or by their address:

sort(contacts, on(Contacts.class).getAddress());

and so on. More in general, it offers a DSL to access and manipulate your collections in many ways, like filtering or grouping your contacts based on some conditions, aggregate some of their property values, etc.

Chainey answered 7/5, 2010 at 13:2 Comment(0)
H
1

Ok, I know this was answered a long time ago... but, here's some new info:

Say the Contact class in question already has a defined natural ordering via implementing Comparable, but you want to override that ordering, say by name. Here's the modern way to do it:

List<Contact> contacts = ...;

contacts.sort(Comparator.comparing(Contact::getName).reversed().thenComparing(Comparator.naturalOrder());

This way it will sort by name first (in reverse order), and then for name collisions it will fall back to the 'natural' ordering implemented by the Contact class itself.

Hoshi answered 23/5, 2020 at 19:41 Comment(0)
N
0

You shoud use the Arrays.sort function. The containing classes should implement Comparable.

Nonesuch answered 28/11, 2009 at 23:20 Comment(1)
Problem is that OP is using ArrayList, not array.Nasya
T
0

The Collections.sort is a good sort implementation. If you don't have The comparable implemented for Contact, you will need to pass in a Comparator implementation

Of note:

The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.

The merge sort is probably better than most search algorithm you can do.

Trimetrogon answered 28/11, 2009 at 23:21 Comment(0)
T
0

I did it by the following way. number and name are two arraylist. I have to sort name .If any change happen to name arralist order then the number arraylist also change its order.

public void sortval(){

        String tempname="",tempnum="";

         if (name.size()>1) // check if the number of orders is larger than 1
            {
                for (int x=0; x<name.size(); x++) // bubble sort outer loop
                {
                    for (int i=0; i < name.size()-x-1; i++) {
                        if (name.get(i).compareTo(name.get(i+1)) > 0)
                        {

                            tempname = name.get(i);

                            tempnum=number.get(i);


                           name.set(i,name.get(i+1) );
                           name.set(i+1, tempname);

                            number.set(i,number.get(i+1) );
                            number.set(i+1, tempnum);


                        }
                    }
                }
            }



}
Tetzel answered 24/8, 2012 at 6:10 Comment(1)
You're going to take longer to write this, get less optimal sort performance, write more bugs (and hopefully more tests), and the code will be harder to transfer to other people. So it's not right. It may work, but it that doesn't make it correct.Mease
M
0

use this method:

private ArrayList<myClass> sortList(ArrayList<myClass> list) {
    if (list != null && list.size() > 1) {
        Collections.sort(list, new Comparator<myClass>() {
            public int compare(myClass o1, myClass o2) {
                if (o1.getsortnumber() == o2.getsortnumber()) return 0;
                return o1.getsortnumber() < o2.getsortnumber() ? 1 : -1;
            }
        });
    }
    return list;
}

`

and use: mySortedlist = sortList(myList); No need to implement comparator in your class. If you want inverse order swap 1 and -1

Mcmath answered 9/11, 2018 at 16:41 Comment(0)
B
0

With java 8 feature

List<Contact> contact = contactArray.stream().sorted((c1, c2) -> ((c1.getName().compareTo(c2.getName())))).collect(Collectors.toList());
Bunghole answered 2/7, 2022 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.