static void Main(string[] args) { Car[] myAutos = new Car[5]; myAutos[0] = new Car("Rusty", 80, 1); myAutos[1] = new Car("Mary", 40, 234); myAutos[2] = new Car("Viper", 40, 34); myAutos[3] = new Car("Mel", 40, 4); myAutos[4] = new Car("Chucky", 40, 5); Console.WriteLine("Unordered cars"); foreach (Car c in myAutos) { Console.WriteLine("{0} {1}", c.CarID, c.PetName); } Console.WriteLine(); Array.Sort(myAutos); Console.WriteLine("Sorted cars"); foreach (Car c in myAutos) { Console.WriteLine("{0} {1}", c.CarID, c.PetName); } Console.WriteLine(); Array.Sort(myAutos, Car.SortByPetName); Console.WriteLine("Sorted cars (by petname)"); foreach (Car c in myAutos) { Console.WriteLine("{0} {1}", c.CarID, c.PetName); } Console.WriteLine(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Object Sorting *****\n"); // Make an array of Car objects. Car[] myAutos = new Car[5]; myAutos[0] = new Car("Rusty", 80, 1); myAutos[1] = new Car("Mary", 40, 234); myAutos[2] = new Car("Viper", 40, 34); myAutos[3] = new Car("Mel", 40, 4); myAutos[4] = new Car("Chucky", 40, 5); // Display current array. Console.WriteLine("Here is the unordered set of cars:"); foreach (Car c in myAutos) Console.WriteLine("{0} {1}", c.CarID, c.PetName); // Now, sort them using IComparable! Array.Sort(myAutos); Console.WriteLine(); // Display sorted array. Console.WriteLine("Here is the ordered set of cars:"); foreach (Car c in myAutos) Console.WriteLine("{0} {1}", c.CarID, c.PetName); Console.ReadLine(); // Now sort by pet name. Array.Sort(myAutos, Car.SortByPetName); // Dump sorted array. Console.WriteLine("Ordering by pet name:"); foreach (Car c in myAutos) Console.WriteLine("{0} {1}", c.CarID, c.PetName); }
static void Main(string[] args) { Car[] myAutos = new Car[5]; myAutos[0] = new Car("Rusty", 80, 1); myAutos[1] = new Car("Mary", 40, 234); myAutos[2] = new Car("Viper", 40, 34); myAutos[3] = new Car("Mel", 40, 4); myAutos[4] = new Car("Chucky", 40, 5); // Display current array. Console.WriteLine("Here is the unordered set of cars:"); foreach (Car c in myAutos) Console.WriteLine("{0} {1}", c.CarID, c.PetName); // Now, sort them using IComparable! Array.Sort(myAutos); Console.WriteLine(); // Display sorted array. Console.WriteLine("Here is the ordered set of cars:"); foreach (Car c in myAutos) Console.WriteLine("{0} {1}", c.CarID, c.PetName); // sorted by petname Console.WriteLine(); //Array.Sort(myAutos, new PetNameComparer()); Array.Sort(myAutos, Car.PetNameComparer); foreach (Car c in myAutos) Console.WriteLine("{0} {1}", c.CarID, c.PetName); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Object Sorting *****\n"); Car[] myAutos = new Car[5]; myAutos[0] = new Car("Rusty", 50, 1); myAutos[1] = new Car("Mary", 40, 234); myAutos[2] = new Car("Viper", 40, 34); myAutos[3] = new Car("Mel", 40, 4); myAutos[4] = new Car("Chucky", 40, 5); Console.WriteLine("***** Here is the unordered set of cars: *****"); foreach (Car c in myAutos) Console.WriteLine("{0} {1}", c.CarID, c.PetName); Array.Sort(myAutos); Console.WriteLine("***** Here is the ordered set of cars: *****"); foreach (Car c in myAutos) Console.WriteLine("{0} {1}", c.CarID, c.PetName); Array.Sort(myAutos, Car.SortByPetName); Console.WriteLine("***** Ordering by pet name *****"); foreach (Car c in myAutos) Console.WriteLine("{0} {1}", c.CarID, c.PetName); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with object sorting *****\n"); //make an array of car objs Car[] myAutos = new Car[5]; myAutos[0] = new Car("Rusty", 80, 1); myAutos[1] = new Car("Mary", 40, 234); myAutos[2] = new Car("Viper", 40, 34); myAutos[3] = new Car("Mel", 40, 4); myAutos[4] = new Car("Chucky", 40, 5); //Sort() cars...no way Jose... //Array.Sort(myAutos);//a run-time error saying that it failed to compare 2 elements in the array //to fix this, we must implement IComparable in our custom type, and then tell CompareTo() what to use for comparison //display the current array Console.WriteLine("Here is the unordered set of cars:"); foreach (Car c in myAutos) { Console.WriteLine("{0} {1}", c.CarID, c.PetName); } //now sort dem suckas! with IComparable Array.Sort(myAutos); Console.WriteLine(); //and print the sorted array Console.WriteLine("Here is the ordered set of cars:"); foreach (Car c in myAutos) { Console.WriteLine("{0} {1}", c.CarID, c.PetName); } //now sort by pet name via the PetNameComparer class... Array.Sort(myAutos, new PetNameComparer());//HELP //dump the sorted array Console.WriteLine("Odering by pet name:"); foreach (Car c in myAutos) { Console.WriteLine("{0} {1}", c.CarID,c.PetName); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Object Sorting *****"); Car[] myAutos = new Car[5]; myAutos[0] = new Car("Rusty", 80, 1); myAutos[1] = new Car("Mary", 40, 234); myAutos[2] = new Car("Viper", 40, 34); myAutos[3] = new Car("Mel", 40, 4); myAutos[4] = new Car("Chucky", 40, 5); foreach(Car c in myAutos) Console.WriteLine(c); Array.Sort(myAutos); foreach(Car c in myAutos) Console.WriteLine(c); // 传入实现了IComparer接口的对象,可以自动调用ICompare方法 Array.Sort(myAutos, new PetNameComparer()); foreach(Car c in myAutos) Console.WriteLine(c); }