How to sort an Array in Java

In this post we will show how to sort an array in Java. The examples provided will include an Integer array and also an array of strings.

How to sort an array in Java:

The example below sorts an array of integers in a random order. We have used the Collections sort() method to sort the array in the natural order, which in this case is lowest to highest.

Running the above code produces the following output:

The second example below shows how to sort an array of strings. Again the ordering is the natural ordering which in this case is, ordered alphabetically.

Running the above code produces the following output:

In conclusion I hope this post has helped you solve your development issues.

Similar posts:

  1. How to sort a Map
  2. Convert String to an Integer
  3. Java 8 Comparator examples

References:

  1. Java Arrays API
  2. Java Collections API

 

Leave a Reply