I have an ArrayList that contains a large number of strings. It needs to be sorted in place based on three fields (essentially three substrings) which are Name, Age and Amt. Age is the first substring (position 0-3), Name is second (3-6) and Amt is last (6-10). The order in which these parameters are to be sorted is very important and is as follows:
First perform ascending sort by Name THEN do ascending sort by Age (which actually comes earlier in the substring) and THEN do descending sort by Amt. That's it.
I have this class
public class ArrComparer : IComparer
{
public int Compare(object x, object y)
{
string left = x.ToString();
string right = y.ToString();
string lhs = left.Substring(3, 6);
string rhs = right.Substring(3, 6);
return lhs.CompareTo(rhs);
}
}
which I use to sort based on just one field - Name by invoking
RecordList.Sort(new ArrComparer());
This lets me sort correctly based on that one field. The question is how can I modify this code to allow me to sort based on all three AT ONCE, in the right order and using proper asc/desc mode?
Any code or tips would be greatly appreciated. (By the way, in case you are wondering using generic List<T>
is not an option in this project).