## How do I reverse the order of array elements?

In this code snippet you’ll learn how to reverse the order of array elements. To reverse to element order will be using the `Collections.reverse()` method. This method requires an argument with `List` type. Because of this we need to convert the array to a `List` type first. We can use the `Arrays.asList()` to do the conversion. And then we reverse it. To convert the `List` back to array we can use the `Collection.toArray()` method.

Let’s see the code snippet below:

``````package org.kodejava.util;

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

public class ArrayReverse {
public static void main(String[] args) {
// Creates an array of Integers and print it out.
Integer[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8};
System.out.println("Arrays.toString(numbers) = " +
Arrays.toString(numbers));

// Convert the int arrays into a List.
List<Integer> numberList = Arrays.asList(numbers);

// Reverse the order of the List.
Collections.reverse(numberList);

// Convert the List back to array of Integers
// and print it out.
numberList.toArray(numbers);
System.out.println("Arrays.toString(numbers) = " +
Arrays.toString(numbers));
}
}
``````

The output of the code snippet above is:

``````Arrays.toString(numbers) = [0, 1, 2, 3, 4, 5, 6, 7, 8]
Arrays.toString(numbers) = [8, 7, 6, 5, 4, 3, 2, 1, 0]
``````

## How do I sort string of numbers in ascending order?

In the following example we are going to sort a string containing the following numbers `"2, 5, 9, 1, 10, 7, 4, 8"` in ascending order, so we will get the result of `"1, 2, 4, 5, 7, 8, 9, 10"`.

``````package org.kodejava.util;

import java.util.Arrays;

public class SortStringNumber {
public static void main(String[] args) {
// We have some string numbers separated by comma. First we
// need to split it, so we can get each individual number.
String data = "2, 5, 9, 1, 10, 7, 4, 8";
String[] numbers = data.split(",");

// Convert the string numbers into Integer and placed it into
// an array of Integer.
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}

// Sort the number in ascending order using the
// Arrays.sort() method.
Arrays.sort(intValues);

// Convert back the sorted number into string using the
// StringBuilder object. Prints the sorted string numbers.
StringBuilder builder = new StringBuilder();
for (int i = 0; i < intValues.length; i++) {
Integer intValue = intValues[i];
builder.append(intValue);
if (i < intValues.length - 1) {
builder.append(", ");
}
}
System.out.println("Before = " + data);
System.out.println("After  = " + builder);
}
}
``````

When we run the program we will get the following output:

``````Before = 2, 5, 9, 1, 10, 7, 4, 8
After  = 1, 2, 4, 5, 7, 8, 9, 10
``````

## How do I sort a java.util.Enumeration?

In this code snippet you will see how to sort the content of an `Enumeration` object. We start by creating a random numbers and stored it in a `Vector`. We use these numbers and create a `Enumeration` object by calling `Vector`‘s `elements()` method. We convert it to `java.util.List` and then sort the content of the `List` using `Collections.sort()` method. Here is the complete code snippet.

``````package org.kodejava.util;

import java.util.*;

public class EnumerationSort {
public static void main(String[] args) {
// Creates random data for sorting source. Will use java.util.Vector
// to store the random integer generated.
Random random = new Random();
Vector<Integer> data = new Vector<>();
for (int i = 0; i < 10; i++) {
}

// Get the enumeration from the vector object and convert it into
// a java.util.List. Finally, we sort the list using
// Collections.sort() method.
Enumeration<Integer> enumeration = data.elements();
List<Integer> list = Collections.list(enumeration);
Collections.sort(list);

// Prints out all generated number after sorted.
for (Integer number : list) {
System.out.println("Number = " + number);
}
}
}
``````

An example result of the code above is:

``````Number = 20742427
Number = 163885840
Number = 204704456
Number = 560032429
Number = 601762809
Number = 1300593322
Number = 1371678147
Number = 1786580321
Number = 1786731301
Number = 1856215303
``````

## How do I sort LinkedList elements?

To sort the elements of `LinkedList` we can use the `Collections.sort(List<T> list)` static methods. The default order of the sorting is a descending order.

``````package org.kodejava.util;

import java.util.Collections;

public static void main(String[] args) {

System.out.println("Before sorting:");
System.out.println("===============");
}

// Sort the elements of linked list based on its data
// natural order.

System.out.println("After sorting:");
System.out.println("===============");
}
}
}
``````

The result of the program are:

``````Before sorting:
===============
After sorting:
===============
``````

## How do I reverse the order of LinkedList elements?

To reverse the order of `LinkedList` elements we can use the `reverse(List<?> list)` static method of `java.util.Collections` class. Here is the example:

``````package org.kodejava.util;

import java.util.Collections;

public static void main(String[] args) {

System.out.println("Output in original order:");
System.out.println("=========================");
}

// Reverse the element order in the linked list object.

System.out.println("Output in reverse order:");
System.out.println("=========================");
}
}
}
``````

Here is the output of the program:

``````Output in original order:
=========================