This is a kind of old school, but I went to the IComparer interface.,.
public class SortAlphabetLength : System.Collections.IComparer { public int Compare(Object x, Object y) { if (x.ToString().Length == y.ToString().Length) return string.Compare(x.ToString(), y.ToString()); else if (x.ToString().Length > y.ToString().Length) return 1; else return -1; } }
and then check it out.,.
class Program { static void Main(string[] args) { ArrayList values = new ArrayList() { "A","AA","B","BB","C","CC" }; SortAlphabetLength alphaLen = new SortAlphabetLength(); values.Sort(alphaLen); foreach (string itm in values) Console.WriteLine(itm); } }
output:
A B C AA BB CC
source share