Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ComparableExample
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Student[] students =
- {
- new Student() { NeptunCode = "asd123", Semester = 3 },
- new Student() { NeptunCode = "fbg456", Semester = 1 },
- new Student() { NeptunCode = "wrez41", Semester = 2 },
- new Student() { NeptunCode = "fdgh10", Semester = 5 },
- new Student() { NeptunCode = "egh333", Semester = 2 },
- new Student() { NeptunCode = "qed786", Semester = 4 },
- };
- Array.Sort(students);
- foreach (Student student in students)
- {
- Console.WriteLine(student);
- }
- }
- }
- }
- ------------------------------------------------------
- Osztály
- ------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ComparableExample
- {
- internal class Student : IComparable
- {
- public int Semester { get; set; }
- public string NeptunCode{ get; set; }
- public int CompareTo(object obj)
- {
- Student other = obj as Student;
- if (Semester == other.Semester)
- {
- return NeptunCode.CompareTo(other.NeptunCode);
- }
- if (Semester < other.Semester) return -1;
- return 0;
- }
- public override string ToString()
- {
- return $"{NeptunCode} ({Semester})";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment