// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections; namespace E { public class Address { private string road; private string no; public Address() : this("", "") { } public Address(string road, string no) { this.Road = road; this.no = no; } public string Road { get { return road; } set { road = value; } } public string No { get { return no; } set { no = value; } } public override string ToString() { return (road + " " + no); } } public class RoadNoSorter : IComparer { public int Compare(object x, object y) { Address ax = (Address)x; Address ay = (Address)y; if(ax.Road == ay.Road) { int nox = int.Parse(ax.No.Split(" ".ToCharArray())[0]); int noy = int.Parse(ay.No.Split(" ".ToCharArray())[0]); return (nox - noy); } else { return String.Compare(ax.Road, ay.Road); } } } public class MainClass { public static void Main(string[] args) { Address[] adr = new Address[4]; adr[0] = new Address("xxx", "10 st.th."); adr[1] = new Address("xxx", "3 st.tv."); adr[2] = new Address("xxx", "14 1.th."); adr[3] = new Address("x", "1"); Console.WriteLine("Usorteret:"); foreach(Address a in adr) { Console.WriteLine(a); } Array.Sort(adr, new RoadNoSorter()); Console.WriteLine("Sorteret:"); foreach(Address a in adr) { Console.WriteLine(a); } } } }