Wednesday, August 15, 2018

Best 10 Java Array Sort examples

Arrays Sort Examples

Array Sort Examples in java
Array is a collection of elements stored under a single name. In my previous article, discussed about  array tutorials with examples
Sort is based on ascending or descending order.For numbers,Ascending order means lowest number to highest number, Number will be increasing order
descending order means highest number to lowest number, numbers will be descresing order. For Alphabets, A,B,C is natural order and ascending order, C,B,A is reverse of ascending and descending order.

This post is about various Best Arrays sort examples in java.

Arrays.sort method java examples 

java.util.Arrays.sort method is used to sort the primitive types as well as objects.
public static void sort(T[] a, Comparator c)
parameters are :-
First parameter is any object or primitive
Second is Comparator- this is  function for comparing the values in an array. This allows the order of the array to return.

Sort Array of Numbers in ascending order?

In java, numbers can be represeted in integer,long This example sort integers array ascending order

Integer[] numbers = { 5,3,17,1};
System.out.println(Arrays.toString(numbers));
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
Output is

Original:[5, 3, 17, 1]
Sorted asc order:[1, 3, 5, 17]

How to sort integer array in descending order? 

sort() method without comparator returns the elmenets in ascending order, To sort descending order, Either you need to write own comparator or use Collections.reverseOrder().  Here Comparator is supplied as Collections.reverse() order
Integer[] numbers = { 5,3,17,1};
System.out.println("Original:"+Arrays.toString(numbers));
Arrays.sort(numbers,Collections.reverseOrder());
System.out.println("Sorted desc order:"+Arrays.toString(numbers));  
Original:[5, 3, 17, 1]
Sorted desc order:[17, 5, 3, 1]

How to sort String arrays in ascending order? 

pass string array to sort method, this returns the string array in ascending order
String[] strs = { "one", "two", "three", "four", "five" ,"five"};
System.out.println("Original:"+Arrays.toString(strs));
Arrays.sort(strs);
System.out.println("Sorted asc order:"+Arrays.toString(strs));
Original:[one, two, three, four, five, five]
Sorted asc order:[five, five, four, one, three, two]

How to sort String arrays in descending order?

 pass reverseOrder() comparator to Array.sort method() to order in descending order.
String[] strs = { "one", "two", "three", "four", "five" ,"five"};
System.out.println("Original:"+Arrays.toString(strs));
Arrays.sort(strs,Collections.reverseOrder());
System.out.println("Sorted desc order:"+Arrays.toString(strs));
Output is
Original:[one, two, three, four, five, five]
Sorted desc order:[two, three, one, four, five, five]

How to sort Two dimensional array in java?

Two dimensional Arrays are of arrays contains arrays. You have to write comparator to sort elements in two dimensional array
This will works on all versions of java

int[][] arrays = { { 3, 9 }, { 2, 5 }, {18, 19 }, { 41, 3 }, { 15, 4 } };
System.out.println("Original:"+Arrays.deepToString(arrays));
Arrays.sort(arrays, new Comparator() {
 @Override
 public int compare(int[] o1, int[] o2) {
 return ((Integer) o2[0]).compareTo(o1[0]);
 }
});
System.out.println("Sorted:"+Arrays.deepToString(arrays));

Or in Java 8 use below code 
int[][] arrays = { { 3, 9 }, { 2, 5 }, {18, 19 }, { 41, 3 }, { 15, 4 } };
  System.out.println("Original:"+Arrays.deepToString(arrays));
        Arrays.sort(arrays, Comparator.comparing((int[] arr) -> arr[0])
                .reversed());

  System.out.println("Sorted:"+Arrays.deepToString(arrays));
output is
[[3, 9], [2, 5], [18, 19], [41, 3], [15, 4]]
[[41, 3], [18, 19], [15, 4], [3, 9], [2, 5]]

How to sort and merge of integer arrays in java?

Created new array with size is sum of two arrays length Copy both array objects to new array using arrayCopy Method Finally do sort new array using Arrays.sort() method
Integer array1[] = { 9, 5, 71 };
Integer array2[] = { 21, 50, 1 };
Integer output[] = new Integer[array1.length + array2.length];
System.arraycopy(array1, 0, output, 0, array1.length);
System.arraycopy(array2, 0, output, array1.length, array2.length);
Arrays.sort(output);
System.out.println(Arrays.toString(output));
[1, 5, 9, 21, 50, 71]
How to sort Custom Object Arrays in asending or descending order? Created Employee object with id and salary fields.
public class Employee implements Comparable {
 Integer id;
 Integer salary;

 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public Integer getSalary() {
  return salary;
 }
 public void setSalary(Integer salary) {
  this.salary = salary;
 }
 Employee(Integer id, Integer salary) {
  this.id = id;
  this.salary = salary;
 }
 @Override
 public int compareTo(Employee e) {
  return this.salary - e.salary;
 }
 @Override
 public String toString() {
  return "Employee [id=" + id + ", salary=" + salary + "]";
 }
}
Employee[] emps = new Employee[3];
emps[0] = new Employee(1, 4000);
emps[1] = new Employee(2, 60000);
emps[2] = new Employee(3, 5000);
Sort Employees based on salary ascending order
Arrays.sort(emps);
for (int i = 0; i < emps.length; i++) {
 Employee emp = emps[i];
 System.out.println(emp);
}
Employee [id=1, salary=4000]
Employee [id=3, salary=5000]
Employee [id=2, salary=60000]
Sort Employee based on salary descending order
Arrays.sort(emps, Collections.reverseOrder());
for (int i = 0; i < emps.length; i++) {
 Employee emp = emps[i];
 System.out.println(emp);
}
Output is
Employee [id=2, salary=60000]
Employee [id=3, salary=5000]
Employee [id=1, salary=4000]

Related article


EmoticonEmoticon