Jak seřadit seznam v C# | Sada metod List.Sort() -1

Metoda List.Sort(). se používá k řazení prvků nebo části prvků v seznamu pomocí zadané nebo výchozí implementace IComparer nebo poskytnutého delegáta porovnání pro porovnání prvků seznamu. V seznamu přetížení této metody jsou celkem 4 metody takto:

  1. Seřadit (IComparer)
  2. Seřadit (Int32, Int32, IComparer)
  3. Seřadit ()
  4. Seřadit (porovnání)

Zde budeme diskutovat o prvních dvou metodách.

Metoda řazení (IComparer).

Tato metoda se používá k řazení prvků v celém seznamu pomocí zadaného porovnávače.

Syntax:

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

Zde je porovnávačem implementace IComparer, která se má použít při porovnávání prvků, nebo null pro použití výchozího porovnávače Default.

Výjimky:

    InvalidOperationException: Pokud má porovnávač hodnotu null a výchozí porovnávací nástroj Default nemůže najít implementaci obecného rozhraní IComparable nebo rozhraní IComparable pro typ T. ArgumentException: Pokud implementace porovnávače způsobila chybu během řazení. Porovnávač například nemusí vrátit 0 při porovnávání položky se sebou samým.

Příklad 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> >seznam1 => 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);> > > }> > }> }>

Výstup:

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

Příklad 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> >seznam1 => 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> >seznam)> > {> > foreach> (> string> g> in> list )> > {> > Console.WriteLine(g);> > }> > }> }>

Výstup:

 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 
Metoda List.Sort(Int32, Int32, IComparer).

Tato metoda se používá k řazení prvků v rozsahu prvků v seznamu pomocí zadaného porovnávače.

Syntax:

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

Parametry:

index : Je to nulový počáteční index rozsahu, ve kterém proběhne řazení.

jen : Je to délka rozsahu.

porovnat : Při porovnávání prvků použijte IComparer implementace nebo null pro použití výchozího porovnávače Default.

Výjimky:

    ArgumentOutOfRangeException : Pokud index nebo jen je menší než 0. ArgumentException : If index a počet neurčují platný rozsah v Seznam . InvalidOperationException: If porovnat je nulový.

Příklad:




// 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> >seznam1 => 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> >seznam)> > {> > foreach> (> string> g> in> list)> > {> > Console.WriteLine(g);> > }> > }> }>

Výstup:

 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 

Odkaz: