Java program to Sort long Array


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements