이름은 오름차순, 과목은 내림차순 동점자는 이름(오름차순)으로 설정
class ListPrac { static void Main(string[] args) { List<Grade> myAL = new List<Grade>(); string line; StreamReader file = new StreamReader("List_Sample.txt"); while ((line = file.ReadLine()) != null) { string[] words = line.Split('\t'); Grade userGrade = new Grade(words[0], Convert.ToInt32(words[1]), Convert.ToInt32(words[2]), Convert.ToInt32(words[3])); myAL.Add(userGrade); } file.Close(); while (true) { string strLine = Console.ReadLine(); switch(strLine) { case "PRINT": // 이름 순 출력 myAL.Sort((Grade x, Grade y) => x.Name.CompareTo(y.Name)); break; case "KOREAN": // 국어 성적 순 출력 // Lambda식 myAL.Sort((Grade x, Grade y) => x.Korean == y.Korean ? x.Name.CompareTo(y.Name) : y.Korean - x.Korean); break; case "ENGLISH": // 영어 성적 순 출력 // delegate myAL.Sort(delegate (Grade x, Grade y) { if (y.English == x.English) { return x.Name.CompareTo(y.Name); } else { return y.English.CompareTo(x.English); } }); break; case "MATH": // 수학 성적 순 출력 // comparer myAL.Sort(compare); break; case "QUIT": // 종료 return; default: break; } foreach (Grade obj in myAL) { Console.WriteLine("{0}\t{1}\t{2}\t{3}", obj.Name, obj.Korean, obj.English, obj.Math); } } } static int compare(Grade x, Grade y) { if (x.Math == y.Math) return x.Name.CompareTo(y.Name); else return y.Math - x.Math; } } public class Grade { private String name; private int korean; private int english; private int math; public Grade(string str, int k, int e, int m) { Name = str; Korean = k; English = e; Math = m; } public string Name { get => name; set => name = value; } public int Korean { get => korean; set => korean = value; } public int English { get => english; set => english = value; } public int Math { get => math; set => math = value; } } |
'프로그래밍 > C#' 카테고리의 다른 글
프로세스와 쓰레드 샘플 (0) | 2024.05.13 |
---|---|
입력 메시지들을 저장하는 Queue 작성 (0) | 2024.05.13 |
Map을 이용하여 회사원의 투입플젝 확인 하기 (0) | 2024.05.13 |
특정 파일을 모니터링하여 새로운 라인이 쓰일 때 읽는 코드 (0) | 2024.05.13 |
디렉토리와 파일 명 가져오기 (File IO) (0) | 2024.05.13 |