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]


Kindly Share This Post »»

Responses

0 Respones to "Java Program for Sort ArrayList in Reverse Order Java Example"

Post a Comment

 
HOME | Freshers | Exp | Java | SQL | Walkins | OffCampus | BankJobs
=*= PRIVACY POLICY And DISCLAIMER =*=
Some of the stuff contained this site are found on internetThis site is not responsible for publishing all available Information as well accuracy, please check posted Information with its original sources, e.g. News Papers/ Websites etc.We collect all the Information form the Internet.
Software My Zimbio
Copyright © 2013 Career+ Blog +Google | Disclaimer | Privacy Policy | Contact