package com.usr.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortingArrayListInDescendingOrder {
public static void main(String[] args) {
List<String> strList=new ArrayList<String>();
strList.add("sunday");
strList.add("monday");
strList.add("tueday");
strList.add("wednesday");
strList.add("thurday");
strList.add("friday");
strList.add("saturday");
System.out.println("Before Sorting");
System.out.println("...............");
displayList(strList);
System.out.println(".............");
Comparator<String> comparator=Collections.reverseOrder();
Collections.sort(strList, comparator);
System.out.println("after sorting in descending order");
System.out.println("...............");
displayList(strList);
}
private static void displayList(List<String> list) {
for(String temp:list){
System.out.println(temp);
}
}
}
OUTPUT
Before Sorting
...............
sunday
monday
tueday
wednesday
thurday
friday
saturday
.............
after sorting in descending order
...............
wednesday
tueday
thurday
sunday
saturday
monday
friday
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortingArrayListInDescendingOrder {
public static void main(String[] args) {
List<String> strList=new ArrayList<String>();
strList.add("sunday");
strList.add("monday");
strList.add("tueday");
strList.add("wednesday");
strList.add("thurday");
strList.add("friday");
strList.add("saturday");
System.out.println("Before Sorting");
System.out.println("...............");
displayList(strList);
System.out.println(".............");
Comparator<String> comparator=Collections.reverseOrder();
Collections.sort(strList, comparator);
System.out.println("after sorting in descending order");
System.out.println("...............");
displayList(strList);
}
private static void displayList(List<String> list) {
for(String temp:list){
System.out.println(temp);
}
}
}
OUTPUT
Before Sorting
...............
sunday
monday
tueday
wednesday
thurday
friday
saturday
.............
after sorting in descending order
...............
wednesday
tueday
thurday
sunday
saturday
monday
friday
No comments:
Post a Comment
Note: only a member of this blog may post a comment.