43

I have code and I have used list to store data. I want to sort data of it, then is there any way to sort data or shall I have to sort it manually by comparing all data?

import java.util.ArrayList; 
import java.util.List;

public class tes
{
    public static void main(String args[])
    {
        List<Integer> lList = new ArrayList<Integer>();
        lList.add(4);
        lList.add(1);
        lList.add(7);
        lList.add(2);
        lList.add(9);
        lList.add(1);
        lList.add(5);
        for(int i=0; i<lList.size();i++ )
        {
            System.out.println(lList.get(i));
        }
    }
}
Alex
  • 31
  • 1
  • 1
  • 7
  • 7
    `Collections.sort(lList);`. If you want to sort by descending order, you should precise a custom comparator. Btw _"java sort list"_ on Google will really give you the result much more faster than asking a question here. – Alexis C. Dec 11 '13 at 11:37

7 Answers7

56

You can use Collections for to sort data:

import java.util.Collections;
import java.util.ArrayList;
import java.util.List;

public class tes
{
    public static void main(String args[])
    {
        List<Integer> lList = new ArrayList<Integer>();

        lList.add(4);       
        lList.add(1);
        lList.add(7);
        lList.add(2);
        lList.add(9);
        lList.add(1);
        lList.add(5);

        Collections.sort(lList);

        for(int i=0; i<lList.size();i++ )
        {
            System.out.println(lList.get(i));
        }

    }
}
Alex
  • 31
  • 1
  • 1
  • 7
Java Curious ღ
  • 3,622
  • 8
  • 39
  • 63
52

Ascending order:

 Collections.sort(lList); 

Descending order:

Collections.sort(lList, Collections.reverseOrder()); 
Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
5

Use Collections class API to sort.

Collections.sort(list);
codingenious
  • 8,385
  • 12
  • 60
  • 90
3

Just use Collections.sort(yourListHere) here to sort.

You can read more about Collections from here.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
3

You are using Lists, concrete ArrayList. ArrayList also implements Collection interface. Collection interface has sort method which is used to sort the elements present in the specified list of Collection in ascending order. This will be the quickest and possibly the best way for your case.

Sorting a list in ascending order can be performed as default operation on this way:

Collections.sort(list);

Sorting a list in descending order can be performed on this way:

Collections.reverse(list);

According to these facts, your solution has to be written like this:

public class tes 
{
    public static void main(String args[])
    {
        List<Integer> lList = new ArrayList<Integer>();
        lList.add(4);
        lList.add(1);
        lList.add(7);
        lList.add(2);
        lList.add(9);
        lList.add(1);
        lList.add(5);

        Collections.sort(lList);
        for(int i=0; i<lList.size();i++ )
        {
            System.out.println(lList.get(i));
        }
     }
}

More about Collections you can read here.

Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45
2

To sort in ascending order :

Collections.sort(lList);

And for reverse order :

Collections.reverse(lList);
rachana
  • 3,344
  • 7
  • 30
  • 49
  • 6
    `Collections.reverse(lList);` will just reverse the elements in the list, not sort in descending order. – Alexis C. Dec 11 '13 at 11:48
0

You can use the utility method in Collections class public static <T extends Comparable<? super T>> void sort(List<T> list) or

public static <T> void sort(List<T> list,Comparator<? super T> c)

Refer to Comparable and Comparator interfaces for more flexibility on sorting the object.

Anugoonj
  • 575
  • 3
  • 9