πŸ” Java How-To Examples
Estimated reading: 3 minutes 31 views

πŸ”’ Java: How to Sort an Array (Ascending & Descending)


🧲 Introduction – Why Sorting Arrays Matters

Sorting is one of the most frequent and critical operations in programming. Whether you’re organizing search results, ranking scores, or optimizing data analysis, sorting arrays is a must-have skill.

βœ… In this guide, you’ll learn:

  • How to sort integer, string, and custom arrays in Java
  • Sort in ascending and descending order
  • Use of Arrays.sort() and Collections.reverseOrder()
  • Best practices for performance and correctness

πŸ“š Required Import

import java.util.Arrays;
import java.util.Collections;

βœ… Explanation:

  • Arrays.sort() is used for sorting arrays.
  • Collections.reverseOrder() is used to sort arrays in descending order (only for object arrays).

πŸ”Ό Method 1: Sort an Integer Array in Ascending Order

import java.util.Arrays;

public class SortIntAsc {
    public static void main(String[] args) {
        int[] numbers = {50, 10, 30, 20, 40};
        Arrays.sort(numbers);

        System.out.println("Sorted Array (Ascending): " + Arrays.toString(numbers));
    }
}

βœ… Explanation:

  • Arrays.sort() arranges elements in ascending order.
  • Works with primitive types like int, char, etc.

πŸ”½ Method 2: Sort Integer Array in Descending Order

import java.util.Arrays;
import java.util.Collections;

public class SortIntDesc {
    public static void main(String[] args) {
        Integer[] numbers = {50, 10, 30, 20, 40};
        Arrays.sort(numbers, Collections.reverseOrder());

        System.out.println("Sorted Array (Descending): " + Arrays.toString(numbers));
    }
}

βœ… Explanation:

  • You must use Integer[] instead of int[] because Collections.reverseOrder() works with objects, not primitives.

πŸ’‘ Tip: Use Integer[] if you need reverse sorting.


πŸ”€ Method 3: Sort a String Array Alphabetically

import java.util.Arrays;

public class SortStrings {
    public static void main(String[] args) {
        String[] names = {"Zara", "Mike", "Alex", "Bob"};
        Arrays.sort(names);

        System.out.println("Sorted Names: " + Arrays.toString(names));
    }
}

βœ… Explanation:

  • Arrays.sort() uses lexicographical order (A-Z).

πŸ” Method 4: Sort a String Array in Reverse Order

import java.util.Arrays;
import java.util.Collections;

public class SortStringsDesc {
    public static void main(String[] args) {
        String[] names = {"Zara", "Mike", "Alex", "Bob"};
        Arrays.sort(names, Collections.reverseOrder());

        System.out.println("Sorted Names (Descending): " + Arrays.toString(names));
    }
}

βœ… Explanation:

  • Reverse alphabetical sorting using Collections.reverseOrder().

πŸ”§ Method 5: Sort Part of an Array

int[] partial = {10, 5, 30, 25, 40, 35};
Arrays.sort(partial, 1, 5);  // Sorts elements from index 1 to 4

βœ… Explanation:

  • Only sorts elements from index 1 (inclusive) to 5 (exclusive).

πŸ“Š Performance Note

Array TypeMethodSort Time Complexity
int[], double[]Arrays.sort()O(n log n)
Integer[], String[]Arrays.sort()O(n log n) (uses TimSort)

πŸ’‘ Note: For large-scale data, consider parallelSort() introduced in Java 8.


πŸ’‘ Best Practices

  • βœ… Use Arrays.sort() for quick and clean sorting
  • βœ… Use Collections.reverseOrder() only with object arrays
  • ⚠️ Avoid converting int[] to Integer[] unless necessary
  • βœ… Use Arrays.toString() to print arrays cleanly

πŸ“Œ Summary – Sorting Arrays in Java

Sorting is a core Java skill used in virtually every application domain. With Arrays.sort() and helper classes, Java makes it easy to sort data quickly and efficiently.

🧾 Key Takeaways:

  • Use Arrays.sort() for most sorting needs.
  • Use Collections.reverseOrder() for descending sort (object arrays).
  • Use Arrays.toString() to print sorted arrays.
  • Optimize performance by avoiding unnecessary boxing/unboxing.

❓FAQs – Sort an Array in Java

❓ How to sort an array in Java?

Use Arrays.sort(array) for ascending order.

❓ How to sort an array in descending order?

Convert to Integer[] or String[], then use:

Arrays.sort(array, Collections.reverseOrder());

❓ Can I sort only part of an array?

Yes, use Arrays.sort(array, fromIndex, toIndex).

❓ What’s the difference between int[] and Integer[]?

int[] is primitive and doesn’t support object-based operations like reverse sorting.

❓ Is sorting stable in Java?

Yes, for object arrays Java uses TimSort, which is stable.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Sort an Array

Or Copy Link

CONTENTS
Scroll to Top