[英]C# Sort a list from the values of another list
hoping you can help me with this. 希望你能帮我这个忙。
Okay I have two lists. 好吧,我有两个清单。
List1 - Gets the names of files without a path (so I can list the names on dynamically created buttons). List1-获取没有路径的文件名(因此我可以在动态创建的按钮上列出名称)。 More so for visual purposes only.
更多信息仅出于视觉目的。
List2 - Stores the actual path to the file. List2-存储文件的实际路径。
Now I have done List1.Sort(); 现在我已经完成了List1.Sort(); and List2.Sort().
和List2.Sort()。 But being they are alphabetical, A direct path from C:/// is different than Hello.png.
但是由于它们是按字母顺序排列的,因此从C:///的直接路径与Hello.png不同。 <(Just an example name)>
<(只是一个示例名称)>
So the problem I am facing is, List1 and 2 aren't identical in where the actual elements are stored. 所以我面临的问题是,List1和2在存储实际元素的位置并不相同。 (Which does make sense).
(这确实有道理)。
So is there a way to like sort List2 to match the same order as List1? 那么有没有办法让List2排序与List1相同的顺序呢? Or vice versa.. So when I click the button, it loads the proper image,etc that it needs to.
反之亦然。因此,当我单击该按钮时,它将加载所需的适当图像等。
I agree with the comments above, use a single list of full paths and use the System.Io.Path.FileName method to order by file name regardless of directory. 我同意上面的评论,使用完整路径的单个列表,并使用System.Io.Path.FileName方法按文件名排序,而不管目录如何。
var list2 = new List<string>() { @"C:\Directory1\B.txt", @"C:\Directory2\A.txt" };
var orderedList = list2.OrderBy(System.IO.Path.GetFileName);
//orderedList[0] is @"C:\Directory2\A.txt"
//orderedList[1] is @"C:\Directory1\B.txt"
Create a sorted dictionary by zipping the two lists. 通过压缩两个列表来创建排序字典。
var sortedDict = new SortedDictionary<string, string>(
list1.Zip(ist2, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.