Сортировка Arraylist, который содержит элементы типа класса Hospital - C#
Формулировка задачи:
У меня есть ArrayList , который содержит элементы типа класса Hospital, как мне написать метод сортировки ArrayList по полю типа double Salary
Листинг программы
- private ArrayList arrayList;
Решение задачи: «Сортировка Arraylist, который содержит элементы типа класса Hospital»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication8
- {
- class Program
- {
- static void Main(string[] args)
- {
- ArrayList myList = new ArrayList()
- {
- new Hospital(){Salary=2,myField1=2,myField2=4},
- new Hospital(){Salary=0,myField1=4,myField2=1},
- new Hospital(){Salary=4,myField1=3,myField2=9}
- };
- Console.WriteLine("Неотсортированный массив:");
- foreach (Hospital h in myList)
- Console.WriteLine("Salary: {0}, myField1: {1}, myField2: {2}",
- h.Salary, h.myField1, h.myField2);
- myList.Sort();
- Console.WriteLine("\nОтсортированный массив:");
- foreach (Hospital h in myList)
- Console.WriteLine("Salary: {0}, myField1: {1}, myField2: {2}",
- h.Salary, h.myField1, h.myField2);
- Console.ReadLine();
- }
- }
- public class Hospital:IComparable
- {
- public double Salary { get; set; }
- public double myField1 { get; set; }
- public double myField2 { get; set; }
- public int CompareTo(object obj)
- {
- if (this.Salary > ((Hospital)obj).Salary)
- return 1;
- if (this.Salary < ((Hospital)obj).Salary)
- return -1;
- else
- return 0;
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д