Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中对列表进行排序的方法#_C#_.net_Sorting - Fatal编程技术网

C# 在C语言中对列表进行排序的方法#

C# 在C语言中对列表进行排序的方法#,c#,.net,sorting,C#,.net,Sorting,我已经在下面编写了选择排序方法。一般来说,我想保留代码,因为这是一个学校练习,但我知道有更多正确的方法来做,就像Linq一样。 除了只对属性编号进行排序外,它工作得很好。我可以看到以下错误: temp = list[i].PersonalNumber; list[i].PersonalNumber = list[posMin].PersonalNumber; list[posMin].PersonalNumber = temp; 有没有办法对列表中每个索引包含的所有属性进行排序?还是我必须为每

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

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;
        }
    }
}
公共静态无效选择排序(列表){
//使用此方法,人员列表按升序排序。
//posMin是min位置的缩写
int posMin,temp;
for(int i=0;i
这绝对不是你应该手工做的事情(除非你在训练你的算法技能:)。这将使您的代码更复杂,更难维护

只要说:

using System.Linq;
这样做:

var sorted = list.OrderByDescending(x => x.PersonalNumber).ToList();
你不需要成为Linq忍者就可以使用它。我也强烈建议开始使用它。我想你会同意这本书很容易阅读,而且很明显它在做什么


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

这绝对不是您应该手动执行的操作(除非您正在培训算法技能:)。这将使您的代码更复杂,更难维护

只要说:

using System.Linq;
这样做:

var sorted = list.OrderByDescending(x => x.PersonalNumber).ToList();
你不需要成为Linq忍者就可以使用它。我也强烈建议开始使用它。我想你会同意这本书很容易阅读,而且很明显它在做什么

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

如果您想对列表进行适当排序,只需将
排序

list.Sort((x, y) => x.PersonalNumber.CompareTo(y.PersonalNumber));
要按降序排序,请添加
-

list.Sort((x, y) => -x.PersonalNumber.CompareTo(y.PersonalNumber));
如果您想对列表进行排序,只需将
排序

list.Sort((x, y) => x.PersonalNumber.CompareTo(y.PersonalNumber));
要按降序排序,请添加
-

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

对于大多数情况,您应该使用一种内置功能进行排序,例如
List.Sort
Enumerable.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;
        }
    }
}

对于大多数情况,您应该使用一种内置功能进行排序,例如
List.Sort
Enumerable.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;
        }
    }
}

你可以使用Linq,它会用简单的标准为你排序。嗨,我还是个新手。我离开林克待会。我仍在学习基础知识。Linq是这类事情的朋友:-p为什么不交换Person对象本身而不是它们的属性<代码>临时=列表[i];列表[i]=列表[posMin];列表[posMin]=温度@MaxModestoWallin
temp
必须是
人,而不是
int
如果你这样做。你可以使用Linq,它会用简单的标准为你排序。嗨,我还是个新手。我离开林克待会。我仍在学习基础知识。Linq是这类事情的朋友:-p为什么不交换Person对象本身而不是它们的属性<代码>临时=列表[i];列表[i]=列表[posMin];列表[posMin]=温度
@MaxModestoWallin
temp
必须是
Person
,而不是
int
。您好,谢谢您的回答。我想保留原来的编码,因为这是一个学校的练习,但我会和林克一起去。嗨,谢谢你的回答。我想保留原来的编码,因为这是一个学校的练习,但我会选择Linq。