reverse sort array java code example

Example 1: how to reverse a list in java

List<String> stringList = Arrays.asList("A", "B", "C");
    Collections.reverse(stringList);
    assertThat(stringList).containsExactly("C", "B", "A");

Example 2: java sort array descending

Integer[] cubes = new Integer[] { 8, 27, 64, 125, 256 };
Arrays.sort(cubes, Collections.reverseOrder());

Read more: https://www.java67.com/2016/07/how-to-sort-array-in-descending-order-in-java.html#ixzz6MX60xZLb

Example 3: Java reverse string array

// reverse string array in java using collections
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ReverseStringArrayUsingCollections
{
   public static void main(String[] args)
   {
      // creating list of strings
      List<String> li = new ArrayList<String>();
      li.add("java");
      li.add("core");
      li.add("world");
      li.add("hello");
      System.out.println("Given list: " + li);
      Collections.reverse(li);
      System.out.println("After using collections: " + li);
   }
}

Tags:

Java Example