ArrayList sort android
Sorting is an essential part of any programming language. Java, no doubt is one of the best programming languages. It has various features that make it easy! This article will help you unfold everything about sorting in Java especially, sort Array, ArrayList, String, List, Map and Set in Java. Show Below topics are covered in this sorting article:
Lets begin. :-) Sort Array in JavaArrays in Java store one or more values of a specific data type and provide indexed access to store the same in a single variable. Lets look at the below program to sort an array in Java in ascending order. However, ensure you have Java installed. Sort Array in Java Ascending Orderpackage Edureka; import java.util.Arrays; public class SortArray { public static void main(String[] args) { int[] arr = {52,12,2,72,4}; // array of 5 elements Arrays.sort(arr); System.out.printf("Sorted arr[] = %s", Arrays.toString(arr)); } }Output Sorted arr[] = [2, 4, 12, 52, 72] Sort Array in Java Descending Orderpackage Edureka; import java.util.Arrays; import java.util.Collections; public class SortArray { public static void main(String[] args) { Integer[] arr = {52,12,2,72,4}; // used Integer[] instead of int as collections Arrays.sort(arr, Collections.reverseOrder()); // reverseorder() for descending order System.out.printf("Sorted arr[] = %s", Arrays.toString(arr)); } }Output: Sorted arr[] = [72, 52, 12, 4, 2] Note: In the above code, I have used Integer[] array instead of int because of collections. This is because reverseOrder() doesnt support primitive types. Many people confuse with the concept of arrays and ArrayList in Java. Below table may clear all your doubts.
Sort ArrayList in Java (String)Sorting ArrayList in Java can be easily done by using a simple sort() method. Refer the below code to sort ArrayList in Java. package Edureka; import java.util.Arrays; import java.util.Collections; import java.util.*; public class sortingarraylist { public static void main(String args[]) { ArrayListOutput Unsorted ArrayList: [sorting, java, arraylist, in] Moving ahead with sorting in Java article, lets see how you can sort integers. Lets try to implement sorting using a different method i.e using Collections.sort() method. Sort ArrayList in Java Using Collections (Integers)You can sort Integer ArrayList using Collections.sort() method. package Edureka; import java.util.Arrays; import java.util.Collections; import java.util.*; public class SortingArrayList { public static void main(String args[]){ ArrayListOutput Sort String in JavaString in Java is immutable. There is no direct method to sort a string in Java. You can use Arrays, which has a method CharArray() that will create a char input string and using another method (Arrays.sort(char c[]) , we can easily sort. package Edureka; import java.util.Arrays; import java.util.Collections; import java.util.*; public class SortingString { public static String sortString(String inputString) { char Array1[] = inputString.toCharArray(); // converting input string to char array Arrays.sort(Array1); return new String(Array1); // return sorted string } public static void main(String[] args) { String inputString = "Edureka"; String outputString = sortString(inputString); System.out.println("Input String : " + inputString); System.out.println("Output String : " + outputString); } }Output Sort a List In JavaTo sort a list in Java, you can use Collections.sort() method. Refer the following code for more understanding: package Edureka; import java.util.Arrays; import java.util.Collections; import java.util.*; public class SortingList { public static void main(String[] args) { Integer[] digits = new Integer[] {12,56,89,27,22,4,88,65,36}; ListOutput: Sorted String :[4, 12, 22, 27, 36, 56, 65, 88, 89] Sort a Map in JavaA Map in Java belongs to Java collection which contains key-value pair. Therefore, a Map can be sorted in two different ways:
Sort by Key:package Edureka; import java.util.Arrays; import java.util.Collections; import java.util.*; public class SortingMap { public static void main(String[] args) { HashMapOutput: {2=Rachit, 5=Anamika, 14=Aayushi, 30=Amit} Sort by Value:package Edureka; import java.util.Arrays; import java.util.Collections; import java.util.*; public class SortingMap { public static void main(String[] args) { HashMapOutput: {14=Aayushi, 60=Amit, 70=Anamika, 20=Rachit} Moving ahead with sorting in Java, lets pick up the last topic i.e sort a Set in Java. Sort Set in JavaA Set in Java is an interface which extends Collections. It is an unordered collection of objects which does not store duplicate values. Now there is no direct method to sort a Set in Java. Now to sort a set, you have to convert a set to List and then use collections.sort() API and again convert back the list to a set. Refer the below code for more understanding: package Edureka; import java.util.Arrays; import java.util.Collections; import java.util.*; public class SortSet { public static void main(String[] args) { //Unsorted list HashSetOutput: [4, 12, 22, 27, 36, 56, 65, 88, 89] This brings us to the end of our blog on sorting in Java where you have learned how to sort Array, ArrayList, String, Map and Set in Java. I hope you found this blog informative and added value to your knowledge. Make sure you practice as much as possible and revert your experience. Check out theJava Certification Trainingby Edureka,a trusted online learning companywith a network of more than250,000satisfied learnersspread acrossthe globe. Edurekas Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring. Got a question for us? Please mention it in the comments section of this Sorting in Java: Array, ArrayList, String, Map and Set in Java and we will get back to you as soon as possible. |