/* Sort LinkedList In Descending Order Example, sorting linked list java example, sorting linked list java alphabetically, sorting linked list java code, How to sort LinkedList using Comparator */
import java.util.LinkedList;
import java.util.Collections;
import java.util.Comparator;
public class SortLinkedListExample
{
public static void main(String[] args)
{
//create an java.util.LinkedList object
java.util.LinkedList ll = new java.util.LinkedList();
//Add elements to LinkedList
ll.add("A");
ll.add("B");
ll.add("C");
ll.add("D");
ll.add("E");
/*
To get comparator that imposes reverse order on a Collection use
static java.util.Comparator reverseOrder() method of java.util.Collections class
*/
java.util.Comparator comparator = java.util.Collections.reverseOrder();
System.out.println("Before sorting LinkedList in descending order: "+ll);
/*
To sort an LinkedList using comparator use,
static void sort(java.util.List list,java.util.Comparator c) method of java.util.Collections class.
*/
java.util.Collections.sort(ll,java.util.comparator);
System.out.println("After sorting LinkedList in descending order: "+ll);
}
}
Output:
Before sorting LinkedList in descending order: [A, B, C, D, E]
After sorting LinkedList in descending order: [E, D, C, B, A]
import java.util.LinkedList;
import java.util.Collections;
import java.util.Comparator;
public class SortLinkedListExample
{
public static void main(String[] args)
{
//create an java.util.LinkedList object
java.util.LinkedList ll = new java.util.LinkedList();
//Add elements to LinkedList
ll.add("A");
ll.add("B");
ll.add("C");
ll.add("D");
ll.add("E");
/*
To get comparator that imposes reverse order on a Collection use
static java.util.Comparator reverseOrder() method of java.util.Collections class
*/
java.util.Comparator comparator = java.util.Collections.reverseOrder();
System.out.println("Before sorting LinkedList in descending order: "+ll);
/*
To sort an LinkedList using comparator use,
static void sort(java.util.List list,java.util.Comparator c) method of java.util.Collections class.
*/
java.util.Collections.sort(ll,java.util.comparator);
System.out.println("After sorting LinkedList in descending order: "+ll);
}
}
Output:
Before sorting LinkedList in descending order: [A, B, C, D, E]
After sorting LinkedList in descending order: [E, D, C, B, A]
1 comments:
visit java linkedlist examples
http://www.javaproficiency.com/2015/05/java-linkedlist-examples.html
Post a Comment