beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Sort an Array in Descending (Reverse) Order – Java

By Chaitanya Singh | Filed Under: Java Tutorials

The previous tutorial was all about sorting an array in ascending order. In this post we are going to learn how to sort an array in Descending (Reverse) Order.

Example

Here we have two arrays, one is integer array and another one is String array. We are sorting both the arrays in reverse order.

import java.util.Arrays;
import java.util.Collections;
class SortArrayExample {
 
  public static void main(String[] args) {
    // int Array
    Integer[] intArray = new Integer[] {
        new Integer(15),
        new Integer(9),
        new Integer(16),
        new Integer(2),
        new Integer(30)
    };

    // Sorting int Array in descending order
    Arrays.sort(intArray, Collections.reverseOrder());
 
    // Displaying elements of int Array
    System.out.println("Int Array Elements in reverse order:");
    for (int i = 0; i < intArray.length; i++)
       System.out.println(intArray[i]);
 
    // String Array
    String[] stringArray = 
       new String[] { "FF", "PP", "AA", "OO", "DD" };

    // Sorting String Array in descending order
    Arrays.sort(stringArray, Collections.reverseOrder());

    // Displaying elements of String Array
    System.out.println("String Array Elements in reverse order:");
    for (int i = 0; i < stringArray.length; i++)
       System.out.println(stringArray[i]);
  }
}

Output:

Int Array Elements in reverse order:
30
16
15
9
2
String Array Elements in reverse order:
PP
OO
FF
DD
AA

Enjoyed this post? Try these related posts

  1. Java Serialization
  2. Sorting char array in Java example
  3. @Deprecated annotation in java
  4. Java – Finding minimum and maximum values in an array
  5. Sorting double array in Java example
  6. Java Regular Expressions (java regex) Tutorial with examples

Comments

  1. niks says

    February 22, 2017 at 5:01 PM

    Reverse order can be done by sorting it first using Arrays.sort() and then just reverse them.

    Reply

Leave a Reply Cancel reply

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

Java Tutorial

  • Java Index
  • Java Introduction
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Control Statements

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor
  • Java Interview Q

MORE ...

  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap