C#按另一个列表排序列表 [英] C# Sorting list by another list
本文介绍了C#按另一个列表排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在有2个列表:
list<string> names;
list<int> numbers;
,我需要根据数字中的值对我的名字进行排序. 我一直在搜索,并且大多数使用x.ID之类的东西,但是我真的不知道该值是多少.所以那行不通.
and I need to sort my names based on the values in numbers. I've been searching, and most use something like x.ID, but i don't really know what that value is. So that didn't work.
有人知道吗?该在ID部分帮助我吗?
Does anyone know, what to do, or can help me out in the ID part?
推荐答案
所以我假设两个列表中的元素都是通过索引关联的.
So i assume that the elements in both lists are related through the index.
names.Select((n, index) => new { Name = n, Index = index })
.OrderBy(x => numbers.ElementAtOrDefault(x.Index))
.Select(x => x.Name)
.ToList();
但是,如果两个列表都相关,我将使用诸如Dictionary<int,string>
的另一种收集类型.
But i would use another collection type like Dictionary<int,string>
instead if both lists are related insomuch.
这篇关于C#按另一个列表排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文