Here is a simple sorting program of an ArrayList:
ArrayList list = new ArrayList();
list.add(\"1_Update\");
list.add(\"11_Add\")
You can add the IComparable interface and then sort by a specific property. If you have a collections of items of a store for example maybe you want to sort them by price or by category etc. If you want to order by name here is an example:
note how the ArrayList is sorted by the name property of the items. If you do not add the IComparable then when you use the sort method it will throw an error.
static void Main(string[] args)
{
ArrayList items = new ArrayList();
items.Add(new Item("book", 12.32));
items.Add(new Item("cd", 16.32));
items.Add(new Item("bed", 124.2));
items.Add(new Item("TV", 12.32));
items.Sort();
foreach (Item temp in items)
Console.WriteLine("Name:{0} Price:{1}", temp.name, temp.price);
Console.Read();
}
class Item: IComparable
{
public string name;
public double price;
public Item(string _name, double _price)
{
this.name = _name;
this.price = _price;
}
public int CompareTo(object obj)
{
//note that I use the name property I may use a different one
int temp = this.name.CompareTo(((Item)obj).name);
return temp;
}
}
You could write a custom comparator:
Collections.sort(list, new Comparator<String>() {
public int compare(String a, String b) {
return Integer.signum(fixString(a) - fixString(b));
}
private int fixString(String in) {
return Integer.parseInt(in.substring(0, in.indexOf('_')));
}
});
It is doing a lexicographic comparison. It compares the first character in each string sorting them. It then compares the second string of those with the same first charater. When it compares the '_' character to a number, it is greater in value than any single number character just like 8 > 7 and a > 9. Remember it is doing a character comparison and not a numeric comparison.
There are ways to implement your own custom sorting routing which may be better than renaming your script names.
If renaming your script names is an option, this may allow other script tools to be used. One format may be
01_create_table.sql 02_create_index.sql 11_assign_privileges.sql
By keeping your first two digits to two characters, the lexicographic comparison will work.
The Collections.sort() method's docs says:
Sorts the specified list into ascending order, according to the natural ordering of its elements.
Which means for Strings that you are going to get the list in alphabetic order. The String 11_assign_privileges.sql comes before the string 1_create_table.sql and 12_07_insert_static_data.sql comes before 1_create_table.sql etc. So the program is working as expected.
The string compare algorithm compare each character at a time. 1
sorts before 2
. It doesn't matter that it is followed by a 1
or a 2
.
So 100
would sort before 2
. If you don't want this behavior, you need a compare algorithm that handles this case.
As stated above, you are looking for a Comparator implementation that implements a natural sort. Jeff Atwood wrote an excellent post on natural sorting some time ago - it's well worth a read.
If you're looking for a Java implementation I have found this one to be useful: http://www.davekoelle.com/alphanum.html