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));
}
}
}
To sort in ascending order :
And for reverse order :
Use Collections class API to sort.
You can use the utility method in
Collections
classpublic static <T extends Comparable<? super T>> void sort(List<T> list)
orRefer to
Comparable
andComparator
interfaces for more flexibility on sorting the object.Just use
Collections.sort(yourListHere)
here to sort.You can read more about Collections from here.
You can use
Collections
for to sort data:Ascending order:
Descending order: