import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Java Program to sort List of object in reverse order. We will see examples of
* sorting Array List in reverse order of both default ordering as well as any
* custom ordering imposed by Compartor.
*
* @author Javin Paul
*/
public class CollectionSorting {
private static final Logger logger = LoggerFactory.getLogger(CollectionSorting.class);
public static void main(String args[]) {
// Creating and initializing Collection to sort in reverse order
// Integer will be sorted in reverse numeric order
List<Integer> collection = new ArrayList<Integer>();
collection.add(101);
collection.add(201);
collection.add(105);
collection.add(302);
logger.debug("Current order in Collection : " + collection);
// Sorting Collection in reverse order
Collections.sort(collection, Collections.reverseOrder());
logger.debug("Sorted on Reverse order in Collection : {}", collection);
// Sorting List of String in reverse Order
List<String> alphabets = Arrays.asList("A", "B", "C", "D");
logger.debug("Current order in List of String : {}", alphabets);
// Sorting List in reverse Order
Collections.sort(alphabets, Collections.reverseOrder());
logger.debug("List of String sorted in reverse order {}", alphabets);
}
}
Output
[main] DEBUG CollectionSorting - Current order in Collection : [101, 201, 105, 302]
[main] DEBUG CollectionSorting - Sorted on Reverse order in Collection : [302, 201, 105, 101]
[main] DEBUG CollectionSorting - Current order in List of String : [A, B, C, D]
[main] DEBUG CollectionSorting - List of String sorted in reverse order [D, C, B, A]
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Java Program to sort List of object in reverse order. We will see examples of
* sorting Array List in reverse order of both default ordering as well as any
* custom ordering imposed by Compartor.
*
* @author Javin Paul
*/
public class CollectionSorting {
private static final Logger logger = LoggerFactory.getLogger(CollectionSorting.class);
public static void main(String args[]) {
// Creating and initializing Collection to sort in reverse order
// Integer will be sorted in reverse numeric order
List<Integer> collection = new ArrayList<Integer>();
collection.add(101);
collection.add(201);
collection.add(105);
collection.add(302);
logger.debug("Current order in Collection : " + collection);
// Sorting Collection in reverse order
Collections.sort(collection, Collections.reverseOrder());
logger.debug("Sorted on Reverse order in Collection : {}", collection);
// Sorting List of String in reverse Order
List<String> alphabets = Arrays.asList("A", "B", "C", "D");
logger.debug("Current order in List of String : {}", alphabets);
// Sorting List in reverse Order
Collections.sort(alphabets, Collections.reverseOrder());
logger.debug("List of String sorted in reverse order {}", alphabets);
}
}
Output
[main] DEBUG CollectionSorting - Current order in Collection : [101, 201, 105, 302]
[main] DEBUG CollectionSorting - Sorted on Reverse order in Collection : [302, 201, 105, 101]
[main] DEBUG CollectionSorting - Current order in List of String : [A, B, C, D]
[main] DEBUG CollectionSorting - List of String sorted in reverse order [D, C, B, A]
Responses
0 Respones to "Java Program for Sort ArrayList in Reverse Order Java Example"
Post a Comment