Arrays Sorting
Table of Contents
In this article, I am going to show, how to sort arrays in java. Arrays.sort(), Arrays.parallelSort() and Collections.sort(Arrays.asList()) methods are using to sort any type of arrays.
- String array
- int array
- short array
- long array
- double array
- float array
- char array
Pre-Requisites
- Java 1.8
- Eclipse IDE
- Maven
String Array Example
Arrays.sort(new String[] { "narayana", "siva", "kumar", "chandra" }); Arrays.parallelSort(new String[] { "narayana", "siva", "kumar", "chandra" }); Collections.sort(Arrays.asList(new String[] { "narayana", "siva", "kumar", "chandra" }));
int Array Example
Arrays.sort(new int[] { 2,4,5 }); Arrays.parallelSort(new int[] { 2,4,5 }) Collections.sort(Arrays.asList(new int[] { 2,4,5 }));
short Array Example
Arrays.sort(new short[] { 2,4,5 }); Arrays.parallelSort(new short[] { 2,4,5 }); Collections.sort(Arrays.asList(new short[] { 2,4,5 }));
long Array Example
Arrays.sort(new long[] { 2,4,5 }); Arrays.parallelSort(new long[] { 2,4,5 }); Collections.sort(Arrays.asList(new long[] { 2,4,5 }));
double Array Example
Arrays.sort(new double[] { 2333332,42323232,5232322.5 }); Arrays.parallelSort(new double[] { 2333332,42323232,5232322.5 }); Collections.sort(Arrays.asList(new double[] { 2333332,42323232,5232322.5 }));
float Array Example
Arrays.sort(new float[] { 2.8f,4.4f,5.7f }); Arrays.parallelSort(new float[] { 2.8f,4.4f,5.7f }); Collections.sort(Arrays.asList(new float[] { 2.8f,4.4f,5.7f }));
char Array Example
Arrays.sort(new char[] { 2,4,5 }); Arrays.parallelSort(new char[] { 2,4,5 }); Collections.sort(Arrays.asList(new char[] { 2,4,5 }));
Example Code
ArraysSorting.java
package com.narayanatutorial.ArraysSorting; import java.util.Arrays; import java.util.Collections; public class ArraysSorting { public static void main(String args[]) { // String array String[] stringArray = new String[] { "narayana", "siva", "kumar", "chandra" }; // int array // int[] intArray = new int[] { 102, 105, 97, 30 }; // short array // short[] shortArray = new short[] { 102, 105, 97, 30 }; // long array // long[] longArray = new long[] { 102, 105, 97, 30 }; // double array // double[] doubleArray = new double[] { 1024332.22, 1052323.43, 9723232, 302321 // }; // float array // float[] floatArray = new float[] { 102.5f, 105.2f, 97.4f, 30.6f }; // char array // char[] charArray=new char[] // {'N','A','R','A','Y','A','N','A','T','U','T','O','R','I','A','L'}; System.out.println("\nBefore Sort \n***********************************"); for (String value : stringArray) { System.out.println(value); } /* string array sorting */ Arrays.sort(stringArray); // Arrays.parallelSort(stringArray); // Collections.sort(Arrays.asList(stringArray)); /* int array sorting */ // Arrays.sort(intArray); // Arrays.parallelSort(intArray); // Collections.sort(Arrays.asList(intArray)); /* short array sorting */ // Arrays.sort(shortArray); // Arrays.parallelSort(shortArray); // Collections.sort(Arrays.asList(shortArray)); /* long array sorting */ // Arrays.sort(longArray); // Arrays.parallelSort(longArray); // Collections.sort(Arrays.asList(longArray)); /* double array sorting */ // Arrays.sort(doubleArray); // Arrays.parallelSort(doubleArray); // Collections.sort(Arrays.asList(doubleArray)); /* float array sorting */ // Arrays.sort(floatArray); // Arrays.parallelSort(floatArray); // Collections.sort(Arrays.asList(floatArray)); /* char array sorting */ // Arrays.sort(charArray); // Arrays.parallelSort(charArray); // Collections.sort(Arrays.asList(charArray)); System.out.println("\nAfter Sort \n***********************************"); for (String value : stringArray) { System.out.println(value); } } }
Output
Before Sort *********************************** narayana siva kumar chandra After Sort *********************************** chandra kumar narayana siva
Get full source code from Git.
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.