Пример #1
0
        int IComparer.Compare(object ob1, object ob2)
        {
            AbstrState s1 = (AbstrState)ob1;
            AbstrState s2 = (AbstrState)ob2;

            return(String.Compare(s1.Continent, s2.Continent));
        }
Пример #2
0
        int IComparer.Compare(object ob1, object ob2)
        {
            AbstrState s1 = (AbstrState)ob1;
            AbstrState s2 = (AbstrState)ob2;

            return(String.Compare(s1.LeaderName, s2.LeaderName));
        }
Пример #3
0
        int IComparer.Compare(object ob1, object ob2)
        {
            AbstrState s1 = (AbstrState)ob1;
            AbstrState s2 = (AbstrState)ob2;

            return(s1.CompareTo(s2));
        }
Пример #4
0
        // Redefinition of the CompareTo method from the IComparable interface
        public int CompareTo(object s)
        {
            AbstrState newS   = s as AbstrState;
            int        result = new int();

            if (this.Population == newS.Population)
            {
                result = 0;
            }
            if (this.Population > newS.Population)
            {
                result = 1;
            }
            if (this.Population < newS.Population)
            {
                result = -1;
            }
            return(result);
        }
Пример #5
0
        int IComparer.Compare(object ob1, object ob2)
        {
            AbstrState s1 = (AbstrState)ob1;
            AbstrState s2 = (AbstrState)ob2;

            int result = new int();

            if (s1.Age == s2.Age)
            {
                result = 0;
            }
            if (s1.Age > s2.Age)
            {
                result = 1;
            }
            if (s1.Age < s2.Age)
            {
                result = -1;
            }
            return(result);
        }
Пример #6
0
        static void Main(string[] args)
        {
            // Republic class.
            Republic republic = new Republic("The Republic of the Congo", "Denis Sassou Nguesso", 5125821, 58, "Africa", 7, 72, 6);

            // Monarchy class.
            Monarchy monarchy = new Monarchy("Belgium", "Philippe", 11358357, 189, "Europe", "The House of Saxe-Coburg and Gotha");

            // Kingdom class.
            Kingdom kingdom = new Kingdom("The Kingdom of Saudi Arabia", "Salman", 33000000, 87, "Asia", "The Sudairi Seven");

            // Array of objects of classes that use IComparable interface
            IComparable[] comparable = new IComparable[3];
            comparable[0] = republic;
            comparable[1] = monarchy;
            comparable[2] = kingdom;

            // Recreation of the array but with the AbstrState class
            AbstrState[] abstrState = new AbstrState[3];
            for (int i = 0; i < comparable.Length; i++)
            {
                abstrState[i] = (AbstrState)comparable[i];
            }

            Console.WriteLine("THE RESULT OF THE 3RD TASK:");
            // Showing the array
            Console.WriteLine("CREATED ARRAY:");
            foreach (AbstrState a in abstrState)
            {
                a.Show();
            }

            // Sorting the array
            Array.Sort(abstrState, new ComparePopulation());

            // Showing the sorted array
            Console.WriteLine("ARRAY AFTER BEING SORTED BY POPULATION:");
            foreach (AbstrState a in abstrState)
            {
                a.Show();
            }

            Array.Sort(abstrState, new CompareName());
            Console.WriteLine("ARRAY AFTER BEING SORTED BY NAME:");
            foreach (AbstrState a in abstrState)
            {
                a.Show();
            }

            Array.Sort(abstrState, new CompareLeaderName());
            Console.WriteLine("ARRAY AFTER BEING SORTED BY LEADER NAME:");
            foreach (AbstrState a in abstrState)
            {
                a.Show();
            }

            Array.Sort(abstrState, new CompareAge());
            Console.WriteLine("ARRAY AFTER BEING SORTED BY AGE:");
            foreach (AbstrState a in abstrState)
            {
                a.Show();
            }

            Array.Sort(abstrState, new CompareContinent());
            Console.WriteLine("ARRAY AFTER BEING SORTED BY CONTINENT:");
            foreach (AbstrState a in abstrState)
            {
                a.Show();
            }

            // Binary search demonstration
            DemostrateBinarySearch(abstrState);


            // Shallow clone
            Republic shallowClone = republic.ShallowCopy();
            // Real clone
            Republic clone = (Republic)republic.Clone();

            Console.WriteLine("Shallow clone of the Republic object:");
            shallowClone.Show();
            Console.WriteLine("Clone of the Republic object:");
            clone.Show();

            Console.WriteLine("Press ENTER to exit");
            Console.ReadLine();
        }