How to sort an ArrayList?

后端 未结 20 2058
有刺的猬
有刺的猬 2020-11-22 06:19

I have a List of doubles in java and I want to sort ArrayList in descending order.

Input ArrayList is as below:

List testList = new Arr         


        
相关标签:
20条回答
  • 2020-11-22 06:48
    //Here is sorted List alphabetically with syncronized
    
    package com.mnas.technology.automation.utility;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.List;
    
    import org.apache.log4j.Logger;
    
    /**
     * @author manoj.kumar
     */
    public class SynchronizedArrayList {
        static Logger log = Logger.getLogger(SynchronizedArrayList.class.getName());
    
        @SuppressWarnings("unchecked")
        public static void main(String[] args) {
    
            List<Employee> synchronizedList = Collections.synchronizedList(new ArrayList<Employee>());
            synchronizedList.add(new Employee("Aditya"));
            synchronizedList.add(new Employee("Siddharth"));
            synchronizedList.add(new Employee("Manoj"));
            Collections.sort(synchronizedList, new Comparator() {
                public int compare(Object synchronizedListOne, Object synchronizedListTwo) {
                    //use instanceof to verify the references are indeed of the type in question
                    return ((Employee) synchronizedListOne).name
                            .compareTo(((Employee) synchronizedListTwo).name);
                }
            }); 
        /*for( Employee sd : synchronizedList) {
        log.info("Sorted Synchronized Array List..."+sd.name);
        }*/
    
            // when iterating over a synchronized list, we need to synchronize access to the synchronized list
            synchronized (synchronizedList) {
                Iterator<Employee> iterator = synchronizedList.iterator();
                while (iterator.hasNext()) {
                    log.info("Sorted Synchronized Array List Items: " + iterator.next().name);
                }
            }
    
        }
    }
    
    class Employee {
        String name;
    
        Employee(String name) {
            this.name = name;
    
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:50
    Collections.sort(testList);
    Collections.reverse(testList);
    

    That will do what you want. Remember to import Collections though!

    Here is the documentation for Collections.

    0 讨论(0)
  • 2020-11-22 06:50

    The following line should do the thick

    testList.sort(Collections.reverseOrder());
    
    0 讨论(0)
  • |*| Sorting an List :

    import java.util.Collections;
    

    |=> Sort Asc Order :

    Collections.sort(NamAryVar);
    

    |=> Sort Dsc Order :

    Collections.sort(NamAryVar, Collections.reverseOrder());
    

    |*| Reverse the order of List :

    Collections.reverse(NamAryVar);
    
    0 讨论(0)
  • 2020-11-22 06:54
      yearList = arrayListOf()
        for (year in 1950 until 2021) {
            yearList.add(year)
        }
    
       yearList.reverse()
        val list: ArrayList<String> = arrayListOf()
    
        for (year in yearList) {
            list.add(year.toString())
        }
    
    0 讨论(0)
  • 2020-11-22 06:55

    Use util method of java.util.Collections class, i.e

    Collections.sort(list)
    

    In fact, if you want to sort custom object you can use

    Collections.sort(List<T> list, Comparator<? super T> c) 
    

    see collections api

    0 讨论(0)
提交回复
热议问题