logo

C# でリストを並べ替える方法 | List.Sort() メソッド セット -1

List.Sort() メソッド は、指定された IComparer 実装またはデフォルトの IComparer 実装、またはリスト要素を比較するための提供された Comparison デリゲートを使用して、List 内の要素または要素の一部を並べ替えるのに使用されます。このメソッドのオーバーロード リストには、次のように合計 4 つのメソッドがあります。



  1. 並べ替え(IComparer)
  2. 並べ替え(Int32、Int32、IComparer)
  3. 選別()
  4. 並べ替え(比較)

ここでは、最初の 2 つの方法について説明します。

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 =>>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 =>>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)>

パラメーター:

索引 : 並べ替えが行われる範囲のゼロから始まる開始インデックスです。

のみ :範囲の長さです。

比較する : 要素を比較する場合は、 I比較者 デフォルトの比較子 Default を使用する場合は実装または null。

例外:

    ArgumentOutOfRangeException : If 索引 または のみ が 0 未満です。 ArgumentException : If 索引 と count は有効な範囲を指定していません リスト 。 InvalidOperationException : の場合 比較する 無効である。

例:




// 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 =>>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>

参照: