Hello all,
I have been looking around for info on sorting ArrayLists and have found a bit, but I am still kinda stuck.
Given this:
How exactly do I do a numeric value sort? I think it involves creating a comparator, but I have not found anything that explains it in a way that I can wrap my hands around it.Code:for (int count = 0; count < numb.size(); count++)
{
System.out.println(numb.get(count));
}
System.out.println("______SORTED OUTPUT______");
//Now that we have an array list full of file data, lets sort it
Collections.sort(numb);
System.out.println("array len is "+numb.size());
for (int count = 0; count < numb.size(); count++)
{
System.out.println(numb.get(count));
}
What the program does is read in some numbers into an ArrayList (numb) and print the list, then sort, then print again.
My output looks like this right now:
40
20
60
10
50
70
100
90
80
______SORTED OUTPUT______
array len is 9
10
100
20
40
50
60
70
80
90
Collections.sort(numb) does sort, but not by numeric value. the sorted output should be this:
10
20
40
50
60
70
80
90
100
I used an ArrayList because the number of user entered numbers is variable.
Any ideas? would it work better to feed the ArrayList into an Array based on the size of the list, then use Array.sort() on it? or would it be easier to come up with a comparator?
And can someone give me a simple expanation of how that works? I have not come across anything like this before, and I have no idea how to write a comparator or even what one looks like...
Thanks
Jeff
