2016-04-07 16 views
0

私は下の選択ソート方法を書いています。私は学校の演習であるので、一般的なコードを保持したいと思いますが、私はLinqのように、それを行う正しい方法があることを理解しています。 プロパティPersonalNumberだけをソートするだけでなく、うまく動作します。C#でリストをソートする方法

temp = list[i].PersonalNumber; 
list[i].PersonalNumber = list[posMin].PersonalNumber; 
list[posMin].PersonalNumber = temp; 

リスト内の各インデックスに含まれるすべてのプロパティを並べ替える方法はありますか?あるいは、各プロパティのために上記のコードを記述する必要がありますか?合計で3つのプロパティがあります。

詳しい方法:

public static void SelectionSort(List<Person> list) { 
    // With this method the Person list is sorted in ascending order. 
    //posMin is short for position of min 
    int posMin, temp; 
    for (int i = 0; i < list.Count - 1; i++) { 
     posMin = i;//Set posMin to the current index of array 
     for (int j = i + 1; j < list.Count; j++) { 
      if (list[j].PersonalNumber < list[posMin].PersonalNumber) { 
       //posMin will keep track of the index that min is in, this is needed when a swap happens 
       posMin = j; 
      } 
     } 

     //if pos_min no longer equals i than a smaller value must have been found, so a swap must occur 
     if (posMin != i) { 
      temp = list[i].PersonalNumber; 
      list[i].PersonalNumber = list[posMin].PersonalNumber; 
      list[posMin].PersonalNumber = temp; 
     } 
    } 
} 
+3

、それがソートされます簡単な基準であなたのリスト –

+0

こんにちは、私はまだかなり初心者です。私は後でLinqを残す。私はまだ基​​本を学んでいます。 – Max

+3

Linqはこの "ソート"のためのあなたの友人です:-p http://stackoverflow.com/questions/722868/sorting-a-list-using-lambda-linq-to-objects – ManoDestra

答えて

1

(あなたはアルゴリズミックスのスキルを訓練している場合を除き:))それは間違いなくあなたが手動で行うべきものではありません。コードを複雑にし、保守しにくくします。

だけ置く:

using System.Linq; 

とこれを行うに:

var sorted = list.OrderByDescending(x => x.PersonalNumber).ToList(); 

あなたがそれを使用するためにLINQの忍者である必要はありません。また、使用を開始することを強くお勧めします。私はあなたが読むことが非常に簡単で、何をしているのかがはっきりしていることに同意できると思います。

ああ、昇順でソートする場合は、.OrderByDescendingの代わりに.OrderByを使用してください。

+0

こんにちは、答えに感謝します。私はそれが学校の練習のようにオリジナルのコーディングを保持したいと思いますが、私はLinqと一緒に行くつもりです。 – Max

0

あなたの場所にリストをソートしたい場合は、単にSortを置く:

list.Sort((x, y) => x.PersonalNumber.CompareTo(y.PersonalNumber)); 

-を追加し、降順でソートするに:ほとんどのシナリオで

list.Sort((x, y) => -x.PersonalNumber.CompareTo(y.PersonalNumber)); 
0

を、あなたがすべきですList<T>.SortまたはEnumerable.OrderByのような組み込み機能の1つをソートに使用します。ソートアルゴリズムの独自の実装を保持することを前提としています。

あなたのメソッドの2番目の引数としてキーセレクター関数を導入することができます

public static void SelectionSort<TSource, TKey>(
    List<TSource> list, 
    Func<TSource, TKey> keySelector) 
{ 
    // With this method the list is sorted in ascending order. 
    //posMin is short for position of min 
    int posMin; 
    for (int i = 0; i < list.Count - 1; i++) { 
     posMin = i;//Set posMin to the current index of array 
     for (int j = i + 1; j < list.Count; j++) { 
      if (keySelector(list[j]) < keySelector(list[posMin])) { 
       //posMin will keep track of the index that min is in, this is needed when a swap happens 
       posMin = j; 
      } 
     } 

     //if pos_min no longer equals i than a smaller value must have been found, so a swap must occur 
     TSource temp; 
     if (posMin != i) { 
      temp = list[i]; 
      list[i] = list[posMin]; 
      list[posMin] = temp; 
     } 
    } 
} 

をあなたはその後、ラムダ式でこれを消費することになる:あなたはLINQのを使用することができ

SelectionSort(persons, (Person p) => p.PersonalNumber); 
関連する問題