按提供的特定顺序在 C# 中排序

Ordering list in C# in specific order provided

提问人:user14806829 提问时间:8/16/2023 更新时间:8/18/2023 访问量:106

问:

我正在尝试按提供的特定顺序排列和输出列表的元素。

原文如下:

var outcome = Result.ToArray() 

Result 根据类签名以格式返回元素,如下所示:

class ResultOutcome {
public string id;
public string val;
}

因此,结果将包含以下元素:

Result.ToArray()[0] = {id : "1", val: "100"}
Result.ToArray()[1] = {id : "2", val: "50"}
Result.ToArray()[2] = {id : "3", val: "75"}
Result.ToArray()[3] = {id : "4", val: "98"}
Result.ToArray()[4] = {id : "5", val: "100"}
etc.

我想按照 id 列出的顺序排列结果

int  requiredOrder [] = new int[5] {3,5,2,1,4}

所以结果将是

Result.ToArray()[0] = {id : "3", val: "75"}
Result.ToArray()[1] = {id : "5", val: "100"}
Result.ToArray()[2] = {id : "2", val: "50"}
Result.ToArray()[3] = {id : "1", val: "100"}
Result.ToArray()[4] = {id : "4", val: "98"}

我尝试按照特定顺序按照订购清单进行操作


var outcome =  Result.ToArray();

int[] sequence = new int[5] { 3,4,1,2,5 };

Dictionary<int, int> sequence IndexedMap = Enumerable.Range(0, sequence .Length - 1).ToDictionary(r => sequence [r], r => r);

However, I am not sure how can I filter after it
C# 数组 列表 ArrayList 哈希图

评论

2赞 Andrew Morton 8/16/2023
这回答了你的问题吗?从其他列表 ID 中对列表进行排序

答:

0赞 rotabor 8/16/2023 #1

如果你的语句完全符合它(所需的顺序由从 N 开始的顺序整数集中的数字表示):

public class ResultOutcomeComparer : IComparer {
    int IComparer.Compare(Object x, Object y) {
        return ((new CaseInsensitiveComparer())
            .Compare(((ResultOutcome)x).id, ((ResultOutcome)y).id));
    }
}
...
Array.Sort(ResultToArray, new ResultOutcomeComparer());
var tmp = new int[requiredOrder.Length];
for (int i = 0; i < requiredOrder.Length; i++) tmp[requiredOrder[i] - N] = i; 
Array.Sort(tmp, ResultToArray);

演示,N = 1:

using System;
using System.Collections;
class ResultOutcome { public string id, val; }
public class ResultOutcomeComparer : IComparer {
    int IComparer.Compare(Object x, Object y) {
        return ((new CaseInsensitiveComparer())
            .Compare(((ResultOutcome)x).id, ((ResultOutcome)y).id));
    }
}
class SortByOrder {
    static void Main() {
        const int ML = 5;
        var ResultToArray = new ResultOutcome[ML] {
            new ResultOutcome() { id = "1", val = "100" }
            , new ResultOutcome() { id = "3", val = "75" }
            , new ResultOutcome() { id = "4", val = "98" }
            , new ResultOutcome() { id = "2", val = "50" }
            , new ResultOutcome() { id = "5", val = "100" }
            };
        var requiredOrder = new int[ML] { 3, 5, 2, 1, 4 };

        Array.Sort(ResultToArray, new ResultOutcomeComparer());
        var tmp = new int[ML];
        for (int i = 0; i < ML; i++) tmp[requiredOrder[i] - 1] = i; 
        Array.Sort(tmp, ResultToArray);

        for (int i = 0; i < ML; i++)
            Console.WriteLine("{0} {1}", ResultToArray[i].id, ResultToArray[i].val);
    }
}
0赞 developer-partners 8/16/2023 #2

其他人已经提出了对你要求的方式进行排序的方法,所以我建议稍微改变一下你的方法,作为需要考虑的事情。不要使用不同的数组(在示例中为 requiredOrder),而是尝试向 ResultOutcome 类添加 sortOrder 字段:

class ResultOutcome {
    public string id;
    public string val;
    public int sortOrder;
}

实例化数组时,为数组中的每个元素设置 sortOrder 字段值:

var Result = new List<ResultOutcome> {
    new ResultOutcome { id = "1", val = "100", sortOrder = 4 },
    new ResultOutcome { id = "2", val = "50", sortOrder = 3 },
    new ResultOutcome { id = "3", val = "75", sortOrder = 1 },
    new ResultOutcome { id = "4", val = "98", sortOrder = 5 },
    new ResultOutcome { id = "5", val = "100", sortOrder = 2 }
};

按 sortOrder 字段对数组进行排序:

var sortedArray = Result.OrderBy(element => element.sortOrder);