gpt4 book ai didi

c# - 按另一个数组中指定的顺序对数组进行排序

转载 作者:太空狗 更新时间:2023-10-30 00:48:02 25 4
gpt4 key购买 nike

我有一个键值对列表,我想根据另一个数组中指定的顺序对其进行排序。

var requiredOrder = new String[] { "PH", "HH", "PR", "SR", "UN", "UD", "WD", "WE", "OT" };


var listToBeSorted = new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("A","PR"),
new KeyValuePair<string, string>("B","PH"),
new KeyValuePair<string, string>("C","HH"),
new KeyValuePair<string, string>("D","WD"),
new KeyValuePair<string, string>("E","OT"),
new KeyValuePair<string, string>("F","UN"),
.....
};

'listToBeSorted' 是一个真正的集合,我为需要根据 'requiredOrder' 数组按值排序的下拉列表获取。

最佳答案

您可以使用 Array.IndexOf()

requiredOrder 数组中项目的索引进行排序
listToBeSorted = listToBeSorted.OrderBy(x => Array.IndexOf(requiredOrder, x.Value)).ToList();

关于c# - 按另一个数组中指定的顺序对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50925345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com