Sorting of Objects

About

Sorting of objects in Java is the process of arranging object instances in a specific order typically ascending or descending based on one or more properties of the object. Unlike primitive types, sorting objects requires defining how they should be compared, as Java does not inherently know which fields or attributes determine their order.

Sorting is not limited to alphabetical or numerical order it can involve multi-level criteria, domain-specific rules, or even complex comparisons like date/time, version numbers, or locale-aware strings.

Arrays

Ascending Order

// Integer array
int[] intArray = {5, 2, 9, 1, 5, 6};
Arrays.sort(intArray);
System.out.println(Arrays.toString(intArray));

// Character array
char[] charArray = {'b', 'a', 'd', 'c'};
Arrays.sort(charArray);
System.out.println(Arrays.toString(charArray));

// Long array
long[] longArray = {5L, 2L, 9L, 1L, 5L, 6L};
Arrays.sort(longArray);
System.out.println(Arrays.toString(longArray));

// String array
String[] stringArray = {"banana", "apple", "cherry"};
Arrays.sort(stringArray);
System.out.println(Arrays.toString(stringArray));

Descending Order

List

Wrapper Primitive

Ascending Order

Descending Order

circle-check

Custom Object - Using Comparator

Consider a Person class below as a custom object

Ascending Order

Descending Order

Custom Object - Using Comparable interface

Create a Java class Pair that represents a pair of integers and provides functionality to sort a collection of its objects based on a custom-defined order.

circle-info

Requirements:

  1. Attributes:

    • The Pair class should have two integer attributes: first and second.

  2. Constructor:

    • Implement a parameterized constructor that accepts two integers a and b.

    • Initialize the first attribute with a and the second attribute with b.

  3. Custom Sorting Logic:

    • The class should implement the Comparable interface.

    • Define the sorting order using the compareTo method as follows:

      • Sort by the second attribute in descending order.

      • If the second attributes are equal, sort by the first attribute in descending order.

  4. Example: Given an array or list of Pair objects, the custom sorting should work as follows:

    Input:

    Sorted Output:

Custom Object - Using Lambda expression in Method

Sets

Wrapper Primitive

We usually use a TreeSet to maintain order.

Ascending Order

Descending Order

Custom Objects

Ascending Order

Descending Order

Maps

Maps in Java do not have a natural order, so to sort them we often sort their keys or values and then create a new LinkedHashMap to maintain the order.

Wrapper Primitives

Ascending Order by Keys

Ascending Order by Values

Descending Order by Keys

Descending Order by Values

Custom Object

Person Class

Ascending Order by Keys

Descending Order by Keys

Ascending Order by Values (Person's Age)

Descending Order by Values (Person's Age)

Nested List

Wrapper Primitives

Ascending Order

Descending Order

Custom Object

Ascending Order by Age

Descending Order by Age

Nested Map

Wrapper Primitives

Ascending Order

Descending Order

Custom Object

Assuming Person as keys in nested maps.

Ascending Order

Descending Order

Nested Set

Since Set does not maintain order, we'll use a sorted Set implementation like TreeSet.

Wrapper Primitives

Ascending Order

Descending Order

Custom Object

Assuming Person as elements in sets.

Ascending Order

Descending Orde

Nested List of Set

Wrapper Primitives

Ascending Order

Descending Order

Custom Objects

Person Class

Ascending Order

Descending Order

Nested List of Map

Wrapper Primitives

Ascending Order

Descending Order

Custom Object

Ascending Order

Descending Order

Last updated