コード例 #1
0
        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();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: wordtinker/c-sharp
        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);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: newhuaszh/WPF
        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();
        }
コード例 #4
0
        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();
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: lrymal/Self-Study-C-Sharp
        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();
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: ZLLselfRedeem/zllinmitu
        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);
        }