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));
}
}
}
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:
Sorting a list in descending order can be performed on this way:
According to these facts, your solution has to be written like this:
More about Collections you can read here.