Sorting Collection of Objects by Multiple Fields in Java
Sorting Collections of Objects by Multiple Fields in Java
When working with collections of objects, such as Arrays and ArrayLists, it can be useful to sort the elements by multiple fields rather than a single field. While sorting an Array or ArrayList is fairly simple in Java, sorting by multiple fields can be a bit more complex. Thankfully, there are methods for sorting by multiple fields with the Comparator class.
Sorting Objects with Comparator
The Comparator interface is a powerful tool for sorting collections in Java. It allows you to create custom comparison logic for sorting objects. This makes it ideal for sorting collections by multiple fields.
To sort a collection of objects by multiple fields, you will create a Comparator class that compares each field in turn. Depending on your needs, you may need to create custom Comparator classes for each field or you may be able to reuse a single Comparator class.
Once you have created your Comparator class, you simply need to pass it to the collections "sort" method. This will sort the collection by the criteria specified in your Comparator class.
Examples of Sorting Collections by Multiple Fields
Let's take a look at some examples of sorting collections by multiple fields. We'll use a Person class as our example collection. This class has two fields, firstName and lastName.
In the first example, we'll sort the collection on last name first, then first name:
// Create a list of Person objects List<Person> people = new ArrayList<Person>(); people.add(new Person("John","Doe")); people.add(new Person("Jane","Doe")); people.add(new Person("John","Smith")); // Sort the list by last name, then first name Collections.sort(people, new Comparator() { public int compare(Person p1, Person p2) { int cmp = p1.getLastName().compareTo(p2.getLastName()); if (cmp == 0) { cmp = p1.getFirstName().compareTo(p2.getFirstName()); } return cmp; } });
In this example, we create a Comparator class that compares the last names and, if they are equal, then it compares the first names. This will ensure that the list is sorted by last name first and then by first name.
In the second example, we'll sort the same collection by first name first and then by last name:
// Create a list of Person objects List<Person> people = new ArrayList<Person>(); people.add(new Person("John","Doe")); people.add(new Person("Jane","Doe")); people.add(new Person("John","Smith")); // Sort the list by first name, then last name Collections.sort(people, new Comparator() { public int compare(Person p1, Person p2) { int cmp = p1.getFirstName().compareTo(p2.getFirstName()); if (cmp == 0) { cmp = p1.getLastName().compareTo(p2.getLastName()); } return cmp; } });
In this case, we create a Comparator class that compares the first names and, if they are equal, then it compares the last names. This will ensure that the list is sorted by first name first and then by last name.
Conclusion
Sorting collections of objects by multiple fields is a common task in Java. By using the Comparator class, you can easily create custom comparison logic that can be used to sort collections by any set of criteria.