@embiid

Как работать со сортированным списком?

Нужно реализовать собственную функцию Compare для SortedList.
Вопрос, что мне в типы для SortedList закидывать? Он же принимает два параметра - Tkey и Tvalue. Как Tkey - я закинул Student и потом не понимаю ему еще передать? И из-за этого я не могу пройтись по student в foreach

class Student {
		public string Surname { get; set; }
		public string Name { get; set; }
		public double GPA { get; set; }

		public Student(string Surname, string Name, double GPA) {
			this.Name = Name;
			this.Surname = Surname;
			this.GPA = GPA;
		}
	}

	class StudentComparer : IComparer<Student> {
		public int Compare(Student some, Student another) {
			int result = some.GPA.CompareTo(another.GPA);

			if (result == 0)
				result = some.Surname.CompareTo(another.Surname);

			if (result == 0)
				result = some.Name.CompareTo(another.Name);

			return result;
		}
	}

	class Program {
		static void Main() {
			Student SomeOne = new Student("SomeOneov", "SameOne", 82.2);
			SortedList<Student, int> students = new SortedList<Student, int>(new StudentComparer());

			students.Add(SomeOne, 23);

			//Error   CS0030  
			//Cannot convert type 
			//'System.Collections.Generic.KeyValuePair<ProjName.Student, int>' to 'ProjName.Student'
			foreach (Student s in students) { }
		}
	}
  • Вопрос задан
  • 74 просмотра
Решения вопроса 1
freeExec
@freeExec
Участник OpenStreetMap
foreach (var pair in students) 
{ 
  Student s = pair.Key;
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы