Java program to Sort long Array

Java 8Object Oriented ProgrammingProgramming

To sort long array in Java, use the Arrays.sort() method. Let’s say the following is our long array.

long[] arr = new long[] { 987, 76, 5646, 96,8768, 8767 };

To sort the above long array is an easy task in Java. Use the following method.

Arrays.sort(arr);

After that, print the array and you can see that it is sorted.

for (long l : arr) {
   System.out.println(l);
}

The following is the complete example.

Example

Live Demo

import java.util.*;
public class Demo {
   public static void main(String []args) {
      long[] arr = new long[] { 987, 76, 5646, 96,8768, 8767 };
      System.out.println("Unsorted long array:");
      for (long l : arr) {
         System.out.println(l);
      }
      System.out.println("Sorted long array:");
      Arrays.sort(arr);
      for (long l : arr) {
         System.out.println(l);
      }
   }
}

Output

Unsorted long array:
987
76
5646
96
8768
8767
Sorted long array:
76
96
987
5646
8767
8768
raja
Published on 05-Dec-2018 11:10:56
Advertisements