Java – long[] Arrays sorting with example

In this article, we will discuss how to sort long[] array with example

1. long:

  • Size is 8 bytes
  • That’s is 64 bits
  • Its range is -9,223,372,036,854,775,808 to 9,223,372,036,854,755,807

Arrays class has various sort methods for sorting different primitive data-types

2. Sorting long[] Arrays:

  • To sort long[] array, we have 2 variants of sort methods from Arrays class

Method Signature:

public static void sort(long[] a); // full sorting

public static void sort(long[] a, int fromIndex, int toIndex); // partial sorting

3. Sorting method for long[] Arrays :

Sort method

Description

sort(long[]);sorts complete long[] array
sort(lng[], sIndex, eIndex);sorts partial long[] array, as per limits start-index & end-index specified in the method arguments

Let us move forward to discuss both methods for sorting long[] array

4. Example on Full/Partial Sorting using long[] Arrays:

  1. Complete sorting of long[] Arrays
  2. Partial sorting of long[] Arrays

4.1 Complete sorting of long[] Arrays:

  • Here, complete array will be sorted

Method Signature:

public static void sort(long[] a);

SortingCompleteLongArray.java

package in.bench.resources.java.arrays.sorting;

import java.util.Arrays;

public class SortingCompleteLongArray {

	public static void main(String[] args) {

		// sample long[] array
		long[] lngArray = {7120, 970, 330, 6100, 590, 210, 480};

		// before sorting
		System.out.println("Before sorting : ");
		for(long lngValue : lngArray) {
			System.out.print("|" + lngValue + "|");
		}

		// sorting full long[] array
		Arrays.sort(lngArray);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(long lngValue : lngArray) {
			System.out.print("|" + lngValue + "|");
		}
	}
}

Output:

Before sorting :
|7120||970||330||6100||590||210||480|

After sorting :
|210||330||480||590||970||6100||7120|

4.2 Partial sorting of long[] Arrays:

  • This is the another variant to sort array
  • where we can specify start & end limits within long[] array

Method Signature:

public static void sort(long[] a, int fromIndex, int toIndex);

SortingPartialLongArray.java

package in.bench.resources.java.arrays.sorting;

import java.util.Arrays;

public class SortingPartialLongArray {

	public static void main(String[] args) {

		// sample long[] array
		long[] lngArray = {7120, 970, 330, 6100, 590, 210, 480};

		// before sorting
		System.out.println("Before sorting : ");
		for(long lngValue : lngArray) {
			System.out.print("|" + lngValue + "|");
		}

		// sorting partial long[] array
		Arrays.sort(lngArray, 1, 6);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(long lngValue : lngArray) {
			System.out.print("|" + lngValue + "|");
		}
	}
}

Output:

Before sorting :
|7120||970||330||6100||590||210||480|

After sorting :
|7120||210||330||590||970||6100||480|

Explanation:

  • Here, there are 7 long elements in long[] array
  • But, we have sorted long[] array starting from index-1 till index-5 leaving 1st & last element
  • Therefore, 1st and last element remains as it is after sorting and only middle elements are sorted

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String Array sorting with example
Java - double[] Arrays Sorting with example