Java Program to sort Short array


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements