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:
- Complete sorting of long[] Arrays
- 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:
- Byte Arrays sorting
- char Arrays sorting
- short Arrays sorting
- Integer Arrays sorting
- Float Arrays sorting
- Double Arrays sorting
- Long Arrays sorting
- String Arrays sorting
- Java – How to Sort Arrays in Ascending and Descending order ?
- Java – String Arrays sorting in ascending & descending order
- Java – Sorting after merging two String[] Arrays
- Java – Sorting Arrays using Comparable and Comparator interface
References:
- https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Happy Coding !!
Happy Learning !!