BM Balmiki Mandal 06 May 2023 6 views 0 comments Core Java

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&ltPerson> people = new ArrayList&ltPerson>(); 
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&ltPerson> people = new ArrayList&ltPerson>(); 
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.

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

Unlock the Ins and Outs of JavaScript Engines and Runtimes

What is a JavaScript Engine? A JavaScript engine is a program or interpreter which executes JavaScript code....

BM Balmiki Mandal 05 Sep 2023
Implementing HMACSHA256 Algorithm in Java with Key

Introducing HMACSHA256 in Java with Key HMACSHA256 is an authentication algorithm designed to secure data, su...

BM Balmiki Mandal 30 Aug 2023
HMACSHA256 Java Example - Creating Secure Message Authentication

HMACSHA256 Java Example HMACSHA256 is an advanced encryption algorithm that allows developers to create encry...

BM Balmiki Mandal 30 Aug 2023