Пример #1
0
        // Display the cars in the ListView control.
        private void DisplayCars()
        {
            if (Cars == null) return;

            // Make the appropriate comparer.
            CarComparer comparer = new CarComparer();
            if (sortByComboBox.Text == "Name")
                comparer.SortBy = CarComparer.CompareField.Name;
            else if (sortByComboBox.Text == "Max MPH")
                comparer.SortBy = CarComparer.CompareField.MaxMph;
            else if (sortByComboBox.Text == "Horsepower")
                comparer.SortBy = CarComparer.CompareField.Horsepower;
            else
                comparer.SortBy = CarComparer.CompareField.Price;

            // Sort.
            Array.Sort(Cars, comparer);

            // If we're not sorting by name, reverse the array.
            if (sortByComboBox.Text != "Name") Array.Reverse(Cars);

            carListView.Items.Clear();
            foreach (Car car in Cars)
            {
                ListViewItem item = carListView.Items.Add(car.Name);
                item.SubItems.Add(car.MaxMph.ToString());
                item.SubItems.Add(car.Horsepower.ToString());
                item.SubItems.Add(car.Price.ToString("C"));
            }
            foreach (ColumnHeader header in carListView.Columns)
            {
                header.Width = -2;
            }
        }
        private void compareByChoice(Car[] cars)
        {
            try
            {
                int selection;
                Console.WriteLine("These are the properties you can compare by.");
                foreach (string property in Enum.GetNames(typeof(CarComparer.CompareField)))
                {
                    Console.WriteLine(property.ToString());
                }
                do
                {

                    Console.WriteLine("\nEnter the number of the property you wish to compare with (ascending)");
                    if (!Int32.TryParse(Console.ReadLine(), out selection))
                    {
                        selection = -1;
                    }
                } while (selection < 1 || selection > 4);

                CarComparer comparer = new CarComparer();
                comparer.SortBy = (CarComparer.CompareField)selection;
                Array.Sort(cars, comparer);

                Console.WriteLine("Cars array sorted by " + comparer.SortBy);
                for (int i = 0; i < cars.Length; i++)
                {
                    Console.WriteLine("Cars[" + i + "] = " + cars[i].Name);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Пример #3
0
        /// <summary>
        ///  This method for sorting list of car for loss of quantity of model.
        /// </summary>
        public void SortListCars()
        {
            CarComparer carComparer = new CarComparer();

            cars.Sort(carComparer);
        }
Пример #4
0
        public static void Main(string[] args)
        {
            var car1 = new CarDto
            {
                Id              = Guid.NewGuid(),
                ModelName       = "Cadillac",
                ManufactureDate = DateTime.Now.Subtract(TimeSpan.FromDays(400)),
                Price           = 16000,
                Manufacturer    = new ManufacturerDto
                {
                    Id       = Guid.NewGuid(),
                    Name     = "GM",
                    Contacts = new[]
                    {
                        new ContactDto
                        {
                            Id    = Guid.NewGuid(),
                            Type  = ContactType.Email,
                            Order = 1,
                            Value = "*****@*****.**"
                        },
                        new ContactDto
                        {
                            Id    = Guid.NewGuid(),
                            Type  = ContactType.Phone,
                            Order = 2,
                            Value = "+999999999"
                        },
                        new ContactDto
                        {
                            Id    = Guid.NewGuid(),
                            Type  = ContactType.Post,
                            Order = 3,
                            Value = "Rose street"
                        }
                    }
                },
                WheelCodes = new [] { "001", "002" }
            };

            var car2 = new CarDto
            {
                Id              = Guid.NewGuid(),
                ModelName       = "Ford",
                ManufactureDate = DateTime.Now.Subtract(TimeSpan.FromDays(202)),
                Price           = 16000,
                Manufacturer    = new ManufacturerDto
                {
                    Id       = Guid.NewGuid(),
                    Name     = "Ford motors",
                    Contacts = new[]
                    {
                        new ContactDto
                        {
                            Id    = Guid.NewGuid(),
                            Type  = ContactType.Email,
                            Order = 1,
                            Value = "*****@*****.**"
                        },
                        new ContactDto
                        {
                            Id    = Guid.NewGuid(),
                            Type  = ContactType.Phone,
                            Order = 2,
                            Value = "+888888888"
                        }
                    }
                },
                WheelCodes = new[] { "001", "002", "003" }
            };

            var carComparer = new CarComparer();
            var result      = carComparer.Compare(car1, car2);

            var matchedNames     = result.Matches.Select(r => r.Member.Name).ToArray();
            var missmatchedNames = result.Missmatches.Select(r => r.Member.Name).ToArray();
        }
 public bool Equals(CarComparer other)
 {
     return(object.Equals(GetHashCode(), other.GetHashCode()));
 }