List sorting

In this chapter you will learn:

  1. How to Sort a list
  2. Sort elements of ArrayList
  3. Sort elements of Vector
  4. Sort ArrayList in descending order using comparator

Sort a list

List is a sequence of elements. We can search a list with a default comparator or we can use a specified comparator.

  • static<T extends Comparable<? super T>>void sort(List<T> list)
    Sorts the specified list into ascending order, according to the natural ordering of its elements.
  • static<T> void sort(List<T> list, Comparator<? super T> c)
    Sorts the specified list according to the order induced by the specified comparator.
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
/*j  a va  2  s . c o  m*/
public class Main {
  public static void main(String args[]) {
    LinkedList<Integer> ll = new LinkedList<Integer>();
    ll.add(-8);
    ll.add(20);
    ll.add(-20);
    ll.add(8);

    Comparator<Integer> r = Collections.reverseOrder();

    Collections.sort(ll, r);

    System.out.print("List sorted in reverse: ");
    System.out.print(ll);
  }
}

The output:

Sort elements of ArrayList

In the following code we sort an ArrayList.

import java.util.ArrayList;
import java.util.Collections;
/* j  a  v  a  2 s  . c  om*/
public class Main {
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("java2s.com");
    Collections.sort(arrayList);

    System.out.println(arrayList);
  }
}

The output:

Sort elements of Vector

import java.util.Vector;
import java.util.Collections;
/*from  j  a  v  a 2 s . c om*/
public class Main {
  public static void main(String[] args) {
    Vector v = new Vector();
    v.add("1");
    v.add("3");
    v.add("5");
    v.add("2");
    v.add("java2s .com ");
    Collections.sort(v);

    System.out.println(v);
  }
}

The output:

Sort ArrayList in descending order using comparator

The following code sorts ArrayList in descending order. The control over descending order or ascending order is through different comparators.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/*from  j  a  v a 2  s. co m*/
public class Main {
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("C");
    arrayList.add("D");
    arrayList.add("java2s.com");
    Comparator comparator = Collections.reverseOrder();
    System.out.println("Before: " + arrayList);
    Collections.sort(arrayList, comparator);
    System.out.println("After sorting: " + arrayList);
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to swap element in a list
Home » Java Tutorial » Collections

List interface
List add/insert elements
List clear/remove elements
List search
List element get and set
List and its Iterator
List size, empty
List conversion, to array
List to sublist
List comparison
ArrayList
ArrayList Creation
ArrayList add/insert
ArrayList get/set element
ArrayList clear/remove
ArrayList search
ArrayList copy and shallow copy
ArrayList size, trim to size and capacity
ArrayList to array
LinkedList class
LinkedList creation
LinkedList add/insert elements
LinkedList get elements
LinkedList search
LinkeList replace/set elements
LinkedList remove element
LinkedList copy
LinkedList iterator
LinkedList peek element
LinkedList pop/push element
LinkedList conversion
Map interface
Map element adding
Map.Entry class
Map key
Map value
Map key/value search
Map delete/remove
Map comparison
HashMap Class
HashMap search
HashMap clone
TreeMap
TreeMap key
TreeMap head sub map
TreeMap tail sub map
TreeMap sub map
NavigableMap
NavigableMap key
NavigableMap key-value pair
LinkedHashMap Class
IdentityHashMap
SortedMap
HashSet
HashSet element adding
HashSet element removing and clearing
HashSet clone
HashSet iterator
HashSet properties
TreeSet
TreeSet elements adding
TreeSet subSet
NavigableSet
LinkedHashSet
Iterator
ListIterator
List filling
List reversing
List rotating and shuffling
List sorting
List element swap
List element replacing
List copy
List binary search
Collection unmodifiable
Collection synchronized
Collection singleton
Collection max/min value
Empty Collections
Comparator
Comparable
Enumeration
EnumSet
EnumMap Class
PriorityQueue