C#에서 목록을 정렬하는 방법 | List.Sort() 메서드 세트 -1

List.Sort() 메서드 지정된 IComparer 구현이나 기본 IComparer 구현 또는 목록 요소를 비교하기 위해 제공된 비교 대리자를 사용하여 목록에 있는 요소 또는 요소의 일부를 정렬하는 데 사용됩니다. 이 메서드의 오버로드 목록에는 다음과 같이 총 4개의 메서드가 있습니다.

  1. 정렬(IComparer)
  2. 정렬(Int32, Int32, IComparer)
  3. 종류()
  4. 정렬(비교)

여기서는 처음 두 가지 방법에 대해 설명합니다.

Sort(IComparer) 메서드

이 메서드는 지정된 비교자를 사용하여 전체 목록의 요소를 정렬하는 데 사용됩니다.

통사론:

public void Sort (System.Collections.Generic.IComparer comparer); 

여기서 비교자는 요소를 비교할 때 사용하는 IComparer 구현이거나 기본 비교자 Default를 사용하는 null입니다.

예외:

    InvalidOperationException: 비교자가 null이고 기본 비교자 Default가 IComparable 일반 인터페이스 또는 유형 T에 대한 IComparable 인터페이스의 구현을 찾을 수 없는 경우 ArgumentException: 비교자 구현으로 인해 정렬 중에 오류가 발생한 경우. 예를 들어 비교자는 항목을 자신과 비교할 때 0을 반환하지 않을 수 있습니다.

예시 1:




// C# program to demonstrate the concept of> // List.Sort(IComparer ) method> using> System;> using> System.Collections.Generic;> > class> GFG : IComparer <> int> >> {> > public> int> Compare(> int> x,> int> y)> > {> > if> (x == 0 || y == 0)> > {> > return> 0;> > }> > > // CompareTo() method> > return> x.CompareTo(y);> > > }> }> > public> class> geek> {> > > // Main Method> > public> static> void> Main()> > {> > > // List initialize> > List <> int> >목록1 => new> List <> int> >> > {> > > // list elements> > 1,5,6,2,4,3> > > };> > > Console.WriteLine(> 'Original List'> );> > > foreach> (> int> g> in> list1)> > {> > > // Display Original List> > Console.WriteLine(g);> > > }> > > // 'gg' is the object oif class GFG> > GFG gg => new> GFG();> > > Console.WriteLine(> ' Sort with a comparer:'> );> > > // use of List.Sort(IComparer)> > // method. The comparer is 'gg'> > list1.Sort(gg);> > > foreach> (> int> g> in> list1 )> > {> > > // Display sorted list> > Console.WriteLine(g);> > > }> > }> }>

산출:

 Original List 1 5 6 2 4 3 Sort with a comparer: 1 2 3 4 5 6 

예시 2:




// C# program to demonstrate the concept of> // List.Sort(IComparer ) method> using> System;> using> System.Collections.Generic;> > class> GFG : IComparer <> string> >> {> > public> int> Compare(> string> x,> string> y)> > {> > > if> (x ==> null> || y ==> null> )> > {> > return> 0;> > }> > > // 'CompareTo()' method> > return> x.CompareTo(y);> > > }> }> > public> class> geek> {> > > // Main Method> > public> static> void> Main()> > {> > List <> string> >목록1 => new> List <> string> >();> > > // list elements> > list1.Add(> 'A'> );> > list1.Add(> 'I'> );> > list1.Add(> 'G'> );> > list1.Add(> 'B'> );> > list1.Add(> 'E'> );> > list1.Add(> 'H'> );> > list1.Add(> 'F'> );> > list1.Add(> 'C'> );> > list1.Add(> 'J'> );> > > Console.WriteLine(> 'Original List'> );> > > // Display Original List> > Display(list1);> > > // 'gg' is the object> > GFG gg => new> GFG();> > > Console.WriteLine(> ' Sort with a comparer:'> );> > > // sort the list with a> > // specified comparer 'gg'> > list1.Sort(gg);> > > // Display sorted List> > Display(list1);> > > Console.WriteLine(> ' BinarySearch and Insert D'> );> > > // Binary Search for 'D'> > // using List.BinarySearch(T) method> > int> index = list1.BinarySearch(> 'D'> );> > > if> (index <0)> > {> > > // range++;> > list1.Insert(~index,> 'D'> );> > > }> > > // Display the List after> > // inserting 'D'> > Display(list1);> > > }> > > // Display function> > public> static> void> Display(List <> string> >목록)> > {> > foreach> (> string> g> in> list )> > {> > Console.WriteLine(g);> > }> > }> }>

산출:

 Original List A I G B E H F C J Sort with a comparer: A B C E F G H I J BinarySearch and Insert D A B C D E F G H I J 
List.Sort(Int32, Int32, IComparer) 메서드

이 메서드는 지정된 비교자를 사용하여 List의 요소 범위에 있는 요소를 정렬하는 데 사용됩니다.

통사론:

public void Sort(int index, int len, IComparer comparer) 

매개변수:

색인 : 정렬이 일어날 범위의 0부터 시작하는 인덱스입니다.

오직 : 범위의 길이입니다.

비교하다 : 요소를 비교할 때 다음을 사용하십시오. IComparer 구현 또는 기본 비교자를 사용하려면 null입니다.

예외:

    ArgumentOutOfRangeException : If 색인 또는 오직 0보다 작습니다. ArgumentException : If 색인 개수가 유효한 범위를 지정하지 않았습니다. 목록 . InvalidOperationException : If 비교하다 null입니다.

예:




// C# program to demonstrate the use of> // List.Sort(Int32, Int32, IComparer)> // Method> using> System;> using> System.Collections.Generic;> > class> GFG : IComparer <> string> >> {> > public> int> Compare(> string> x,> string> y)> > {> > if> (x ==> null> || y ==> null> )> > {> > return> 0;> > }> > > // 'CompareTo()' method> > return> x.CompareTo(y);> > > }> }> > public> class> geek> {> > public> static> void> Main()> > {> > List <> string> >목록1 => new> List <> string> >();> > > // list elements> > list1.Add(> 'C++'> );> > list1.Add(> 'Java'> );> > list1.Add(> 'C'> );> > list1.Add(> 'Python'> );> > list1.Add(> 'HTML'> );> > list1.Add(> 'CSS'> );> > list1.Add(> 'Scala'> );> > list1.Add(> 'Ruby'> );> > list1.Add(> 'Perl'> );> > > int> range = 4;> > Console.WriteLine(> 'Original List'> );> > > // Display Original List> > Display(list1);> > > // 'gg' is the object> > GFG gg => new> GFG();> > > Console.WriteLine(> ' Sort a range with comparer:'> );> > > // sort the list within a> > // range of index 1 to 4> > // where range = 4> > list1.Sort(1, range, gg);> > > // Display sorted List> > Display(list1);> > > Console.WriteLine(> ' BinarySearch and Insert Dart'> );> > > // Binary Search and storing> > // index value to 'index'> > int> index = list1.BinarySearch(0, range,> > 'Dart'> , gg);> > > if> (index <0)> > {> > list1.Insert(~index,> 'Dart'> );> > range++;> > }> > > // Display the List> > // after inserting 'Dart'> > Display(list1);> > > }> > > // Display function> > public> static> void> Display(List <> string> >목록)> > {> > foreach> (> string> g> in> list)> > {> > Console.WriteLine(g);> > }> > }> }>

산출:

 Original List C++ Java C Python HTML CSS Scala Ruby Perl Sort a range with comparer: C++ C HTML Java Python CSS Scala Ruby Perl BinarySearch and Insert Dart C++ C Dart HTML Java Python CSS Scala Ruby Perl 

참조: