How to Sort an Arrays of Object using Arrays.sort() Last Updated : 13 Jun, 2024 Comments Improve Suggest changes Like Article Like Report To sort the array of objects in both ascending and descending order in Java, we can use the Arrays.sort() method with the Comparator. In this article, we will learn how to sort an array of objects using Arrays.sort(). Sorting an Array of ObjectWith Array of Object what it really means is an array storing any type of object can be Integer, String or User Defined. Let us check it with an example mentioned below: Input : { { Bob ,25 } , { Charlie , 35 } , { Alice , 30} } Output: Sorting Objects according to age : { { Bob , 25 } , { Alice , 30} , { Charlie , 35 } }Sorting Objects according to name : { { Alice , 30} , { Bob ,25 } , { Charlie , 35 } } Programs for Sorting an Array of Objects Using Arrays.sort():Below is the example using the custom Person class and sorting an array in both ascending order and descending order. Java import java.util.Arrays; import java.util.Comparator; // Person Class class Person { String name; int age; // Constructor Person(String name, int age) { this.name = name; this.age = age; } // Overriding toString Method for the Class @Override public String toString() { return name + " (" + age + ")"; } } // Main Class public class Main { // Main Method public static void main(String[] args) { Person[] people = { new Person("Alice", 30), new Person("Bob", 25), new Person("Charlie", 35) }; // Sort by age in ascending order Arrays.sort(people, new Comparator<Person>() { @Override public int compare(Person p1, Person p2) { return Integer.compare(p1.age, p2.age); } }); System.out.println("Sorting elements according to order by age: " + Arrays.toString(people)); // Sort by name in ascending order Arrays.sort(people, new Comparator<Person>() { @Override public int compare(Person p1, Person p2) { return p1.name.compareTo(p2.name); } }); System.out.println("Sorting elements according to order by name: " + Arrays.toString(people)); } } Output: Ascending order by age: [Bob (25), Alice (30), Charlie (35)]Descending order by age: [Charlie (35), Alice (30), Bob (25)]Ascending order by name: [Alice (30), Bob (25), Charlie (35)]Descending order by name: [Charlie (35), Bob (25), Alice (30)]Explanation of above program: The Person class has two fields called as name and age.It is override toString method to provide the string representation of Person object.A Comparator is implemented the inline using an anonymous class.Arrays.sort (people, new Comparator<Person>) () {...}) is sort the array based on the age in ascending order.The Comparator is modified to the reverse order by swapping the p1 and p2 in compare method.Another Comparator is used to sort an array based on name field in the ascending order.The Comparator is modified to reverse order by swapping the p1 and p2 in compare method for the names. Comment More infoAdvertise with us Next Article How to Sort an Arrays of Object using Arrays.sort() jagan716 Follow Improve Article Tags : Java Java-Collections Practice Tags : JavaJava-Collections Similar Reads How to sort an array of objects by property values ? In this article, we will try to understand how to sort an array of objects by property values in JavaScript with the help of certain examples. Pre-requisite: Array of Objects in JavaScript Example: Input:[ { name: "Ram", age: 17 }, { name: "Mohan", age: 30 }, { name: "Shyam", age: 15 }, { name: "Shy 4 min read How to sort an Array in C# | Array.Sort() Method Set â 2 Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array, Int32, Int32, IComparer) Method Sort(Array, Array, Int32, Int32, IComparer) Method Sort(Array, Int32, Int32) Method 9 min read How to Sort an Array of Object using a Value in TypeScript ? Sorting an array of objects using a value in TypeScript pertains to the process of arranging an array of objects in a specified order based on a particular property or value contained within each object. The below approaches can be used to sort an array of objects using a value in TypeScript: Table 3 min read How to Sort JSON Object Arrays Based on a Key? Sorting is the process of arranging elements in a specific order. In JavaScript, we can sort a JSON array by key using various methods, such as the sort() function. Along with this, we can use various other approaches and methods that are implemented in this article. Below are the approaches to sort 3 min read JavaScript - Sort an Array of Strings Here are the various methods to sort an array of strings in JavaScript1. Using Array.sort() MethodThe sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values.JavaScriptlet a = ['Banana', 3 min read Sort An Array Of Arrays In JavaScript The following approaches can be used to sort array of arrays in JavaScript.1. Using array.sort() Method- Mostly UsedJS array.sort() is used to sort and update the original array in ascending order.JavaScriptlet arr = [[3, 2], [1, 4], [2, 5], [2, 0]]; arr.sort(); console.log(arr);Output[ [ 1, 4 ], [ 2 min read How to sort an Array of Strings in Java Array Of StringsTo sort an array of strings in Java, we can use Arrays.sort() function. Java // A sample Java program to // sort an array of strings // in ascending and descending // orders using Arrays.sort(). import java.util.Arrays; import java.util.Collections; // Driver Class public class SortE 3 min read How to Sort an Array of Objects Based on a Key in JavaScript ? In JavaScript, sorting an array of objects based on the key consists of iterating over the array, applying the sort() method or other approaches, and arranging the array in the desired sorting order. Table of Content Using sort() methodUsing Custom Sorting FunctionUsing Lodash _.orderBy() MethodUsin 3 min read How to Sort an Array of Objects with Null Properties in TypeScript ? Sorting an array of objects in TypeScript involves arranging objects based on specified properties. When dealing with null properties, ensure consistency by placing them either at the start or end of the sorted array to maintain predictability in sorting outcomes. Below are the approaches used to so 5 min read How To Sort An Array Without Mutating The Original Array In JavaScript? Sorting arrays is a common task in JavaScript, especially when you need to present data in a specific order. However, maintaining the original array is often necessary especially if the original data will be required later in your code. In this article we'll see how we can sort an array in JavaScrip 2 min read Like