import java.util.LinkedList;
import java.util.Collections;
import java.util.Comparator;
import static java.lang.System.out;
class SortList2
{
public static void main(String...alt)
{
LinkedList ll=new LinkedList();
ll.add("ravi");
ll.add("altaf");
ll.add("kanta");
ll.add("zahid");
ll.add("nani");
out.println("Before Sorting : "+ll);
Collections.sort(ll,new MyComparator());
out.println("After Sorting : "+ll);
}
}
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
String s1=o1.toString();
String s2=o2.toString();
return -s1.compareTo(s2); //Descending order
}
}
import java.util.Collections;
import java.util.Comparator;
import static java.lang.System.out;
class SortList2
{
public static void main(String...alt)
{
LinkedList ll=new LinkedList();
ll.add("ravi");
ll.add("altaf");
ll.add("kanta");
ll.add("zahid");
ll.add("nani");
out.println("Before Sorting : "+ll);
Collections.sort(ll,new MyComparator());
out.println("After Sorting : "+ll);
}
}
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
String s1=o1.toString();
String s2=o2.toString();
return -s1.compareTo(s2); //Descending order
}
}
0 Comment to "Java Program to sort List(LinkedList) Elements in Descending order(customized sorting order)."
Post a Comment