Java Program to sort Short array

Java 8Object Oriented ProgrammingProgramming

To sort Short array, use the Arrays.sort() method.

Let us first declare and initialize an unsorted Short array.

short[] arr = new short[] { 35, 25, 18, 45, 77, 21, 3 };

Now, let us short the array.

Arrays.sort(arr);

The following is the complete example that shows how to sort Short array in Java.

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String []args) {
      short[] arr = new short[] { 35, 25, 18, 45, 77, 21, 3 };
      System.out.println("Unsorted:");
      for (short s : arr) {
         System.out.println(s);
      }
      System.out.println("Sorted:");
      Arrays.sort(arr);
      for (short s : arr) {
         System.out.println(s);
      }
      System.out.println();
   }
}

Output

Unsorted:
35
25
18
45
77
21
3
Sorted:
3
18
21
25
35
45
77
raja
Published on 12-Nov-2018 17:06:57
Advertisements