How to form ArrayList ascending descending guild
Sorting ArrayList in Java is non difficult, yesteryear using Collections.sort() method yous tin sort ArrayList in ascending in addition to descending guild inwards Java. Collections.sort() method optionally convey a Comparator in addition to if provided it uses Comparator's compare method to compare Objects stored inwards Collection to compare alongside each other, inwards instance of no explicit Comparator, Comparable interface's compareTo() method is used to compare objects from each other. If object's stored inwards ArrayList doesn't implements Comparable than they tin non endure sorted using Collections.sort() method inwards Java.
Sorting ArrayList in Java is non difficult, yesteryear using Collections.sort() method yous tin sort ArrayList in ascending in addition to descending guild inwards Java. Collections.sort() method optionally convey a Comparator in addition to if provided it uses Comparator's compare method to compare Objects stored inwards Collection to compare alongside each other, inwards instance of no explicit Comparator, Comparable interface's compareTo() method is used to compare objects from each other. If object's stored inwards ArrayList doesn't implements Comparable than they tin non endure sorted using Collections.sort() method inwards Java.
Sorting ArrayList inwards Java – Code Example
Here is a consummate code illustration of How to form ArrayList inwards Java, In this Sorting nosotros conduct maintain used Comparable method of String for sorting String on at that topographic point natural order, You tin besides purpose Comparator inwards house of Comparable to form String on whatever other guild than natural ordering e.g. inwards contrary guild yesteryear using Collections.reverseOrder() or inwards instance insensitive guild yesteryear using String.CASE_INSENSITIVE_COMPARATOR.
import java.util.ArrayList;
import java.util.Collections;
/**
*
* Java programme to demonstrate How to form ArrayList inwards Java in both ascending
* in addition to descending guild yesteryear using nub Java libraries.
*
* @author Javin
*/
public class CollectionTest {
public static void main(String args[]) {
//Creating in addition to populating ArrayList inwards Java for Sorting
ArrayList<String> unsortedList = new ArrayList<String>();
unsortedList.add("Java");
unsortedList.add("C++");
unsortedList.add("J2EE");
System.err.println("unsorted ArrayList inwards Java : " + unsortedList);
//Sorting ArrayList inwards ascending Order inwards Java
Collections.sort(unsortedList);
System.out.println("Sorted ArrayList inwards Java - Ascending guild : " + unsortedList);
//Sorting ArrayList inwards descending guild inwards Java
Collections.sort(unsortedList, Collections.reverseOrder());
System.err.println("Sorted ArrayList inwards Java - Descending guild : " + unsortedList);
}
}
Output:
unsorted ArrayList inwards Java : [Java, C++, J2EE]
Sorted ArrayList inwards Java - Ascending guild : [C++, J2EE, Java]
Sorted ArrayList inwards Java - Descending guild : [Java, J2EE, C++]
import java.util.Collections;
/**
*
* Java programme to demonstrate How to form ArrayList inwards Java in both ascending
* in addition to descending guild yesteryear using nub Java libraries.
*
* @author Javin
*/
public class CollectionTest {
public static void main(String args[]) {
//Creating in addition to populating ArrayList inwards Java for Sorting
ArrayList<String> unsortedList = new ArrayList<String>();
unsortedList.add("Java");
unsortedList.add("C++");
unsortedList.add("J2EE");
System.err.println("unsorted ArrayList inwards Java : " + unsortedList);
//Sorting ArrayList inwards ascending Order inwards Java
Collections.sort(unsortedList);
System.out.println("Sorted ArrayList inwards Java - Ascending guild : " + unsortedList);
//Sorting ArrayList inwards descending guild inwards Java
Collections.sort(unsortedList, Collections.reverseOrder());
System.err.println("Sorted ArrayList inwards Java - Descending guild : " + unsortedList);
}
}
Output:
unsorted ArrayList inwards Java : [Java, C++, J2EE]
Sorted ArrayList inwards Java - Ascending guild : [C++, J2EE, Java]
Sorted ArrayList inwards Java - Descending guild : [Java, J2EE, C++]
That's all on How to Sort ArrayList in Java on both ascending in addition to descending order. Just retrieve that Collections.sort() volition form the ArrayList in ascending guild in addition to if yous supply contrary comparator it volition form the ArrayList in descending guild inwards Java.
Further Learning
Java In-Depth: Become a Complete Java Engineer
How to loop or traverse List inwards Java
Further Learning
Java In-Depth: Become a Complete Java Engineer
How to loop or traverse List inwards Java
No comments:
Post a Comment