【问题标题】:Method for sorting a list in C# [duplicate]C#中对列表进行排序的方法[重复]
【发布时间】:2016-04-07 14:57:30
【问题描述】:

我在下面写了选择排序方法。我想保留一般的代码,因为它是一个学校练习,但我知道有更正确的方法可以做到这一点,就像 Linq 一样。 除了只对属性 PersonalNumber 进行排序之外,它还可以很好地工作。我可以看到错误如下:

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

有没有办法对列表中每个索引包含的所有属性进行排序?还是我必须为每个属性编写上述代码?一共有三个属性。

完整方法:

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;
        }
    }
}

【问题讨论】:

  • 你可以使用 Linq,它会用简单的标准为你排序列表
  • 嗨,我还是个新手。我稍后离开 Linq。我还在学习基础知识。
  • Linq 是你的“朋友”:-p stackoverflow.com/questions/722868/…
  • 为什么不交换 Person 对象本身而不是它们的属性呢? temp = list[i]; list[i] = list[posMin]; list[posMin] = temp;
  • @MaxModestoWallin temp 必须是 Person,而不是 int

标签: c# .net sorting


【解决方案1】:

如果要对列表进行排序就地,只需输入Sort

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

要按降序排序,请添加-

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

【讨论】:

    【解决方案2】:

    这绝对不是你应该手动做的事情(除非你正在训练你的算法技能:))。它会使你的代码更复杂,更难维护。

    简单地说:

    using System.Linq;
    

    然后这样做:

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

    您无需成为 Linq ninja 即可使用它。我也强烈建议开始使用它。我想你可以同意它很容易阅读并且很明显它在做什么。

    啊,如果您想要升序排序,只需使用 .OrderBy 而不是 .OrderByDescending。

    【讨论】:

    • 您好,感谢您的回答。我想保留原来的编码,因为这是一个学校练习,但我会选择 Linq。
    【解决方案3】:

    对于大多数情况,您应该使用内置功能之一进行排序,例如List&lt;T&gt;.SortEnumerable.OrderBy。我假设您希望保留自己的排序算法实现。

    您可以引入一个键选择器函数作为方法的第二个参数:

    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;
            }
        }
    }
    

    然后您将使用 lambda 表达式来使用它:

    SelectionSort(persons, (Person p) => p.PersonalNumber);
    

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2010-10-29
      • 2021-12-24
      • 1970-01-01
      相关资源
      最近更新 更多