按索引数组从特定索引中选择数组值

时间:2022-11-25 21:17:01

I want to select array values from specific indexes Now I have this.

我想从特定索引中选择数组值现在我有了这个。

var xs = new[] { 11,12,13,14,15 };
var ind = new[] { 3,2,1,0 };
var results = xs.Where((x, idx) => ind.Contains(idx)).ToArray();

The result is {11,12,13,14} However, I want my result to be ordered by index array which should be {14,13,12,11}

结果是{11,12,13,14}但是,我希望我的结果按索引数组排序,该数组应为{14,13,12,11}

Thank you very much

非常感谢你

2 个解决方案

#1


3  

var results = ind.Select(i => xs[i]).ToArray();

#2


0  

var array = xs.Zip(ind, (x, i) => new Tuple<int, int>(x, i))
           .OrderBy(t => t.Item2)
           .Select(t => t.Item1)
           .ToArray();

#1


3  

var results = ind.Select(i => xs[i]).ToArray();

#2


0  

var array = xs.Zip(ind, (x, i) => new Tuple<int, int>(x, i))
           .OrderBy(t => t.Item2)
           .Select(t => t.Item1)
           .ToArray();