static void Main(string[] args) { car[] Arr = new car[6] { new car("Ford", 1992), new car("Fiat", 1988), new car("Buick", 1932), new car("Ford", 1932), new car("Dodge", 1999), new car("Honda", 1977) }; Console.WriteLine("Array - Unsorted\n"); foreach (car c in Arr) { Console.WriteLine(c.Make + "\t\t" + c.Year); } Array.Sort(Arr); Console.WriteLine("\nArray - Sorted by Make (Ascending - IComparable)\n"); foreach (car c in Arr) { Console.WriteLine(c.Make + "\t\t" + c.Year); } Array.Sort(Arr, car.sortYearAscending()); Console.WriteLine("\nArray - Sorted by Year (Ascending - IComparer)\n"); foreach (car c in Arr) { Console.WriteLine(c.Make + "\t\t" + c.Year); } Console.ReadLine(); }
public static void Main() { car ford = new car(" Mustang", "red", 1993); ford.StartEngine(); Console.ReadLine(); }
int IComparer.Compare(object a, object b) { car c1 = (car)a; car c2 = (car)b; if (c1.year > c2.year) { return(1); } if (c1.year < c2.year) { return(-1); } else { return(0); } }
int IComparable.CompareTo(object obj) { car c = (car)obj; return(String.Compare(this.make, c.make)); }