根据另一个列表对列表进行排序 [英] Sort a list according to another list

查看:59
本文介绍了根据另一个列表对列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的 list1 :

{A,B,C,D,E,F}

我还有另一个 list2 ,其 list2 计数等于 list1 计数(6 = 6):

I have another list2 that list2 count is equal with list1 count (6=6) :

{50,100,14,57,48,94}

我想根据 list2 list1 进行排序,即对 list2 进行升序排序.

I want sort list1 according to list2, that list2 to be sorted ascending.

{14,48,50,57,94,100}

结果:

{C,E,A,D,F,B}

我使用了以下代码.但是结果没有排序

I used the following code. But the result is not sorted

list1= list1.OrderBy(d => list2.IndexOf(d)).ToList();

推荐答案

尝试一下:

int index = 0;
list1 = list1.OrderBy(d => list2[index++]).ToList();

鉴于以下两个列表,它应该产生预期的结果:

It should produce the expected result given the following two lists:

List<string> list1 = new List<string> { "A", "B", "C", "D", "E", "F" };
List<int> list2 = new List<int> { 50, 100, 14, 57, 48, 94 };

这篇关于根据另一个列表对列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆