[英]C# LINQ return counter array indices max to min
A newbie with C# and LINQ. C#和LINQ的新手。 I have an array which is essentially a counted sequence.
我有一个基本上是计数序列的数组。
{1,3,5,2,7,2}
{1,3,5,2,7,2}
I am trying to write a query that returns list of indices with highest values in descending order: 我正在尝试编写一个查询,该查询以降序返回具有最高值的索引列表:
4,2,1,3,5,0
4,2,1,3,5,0
I can get the maximum index with this query below, but I can't seem to work out how to get the next indexes in sequence with a single query. 我可以通过下面的查询获得最大索引,但我似乎无法弄清楚如何使用单个查询按顺序获取下一个索引。
int index = array.ToList().IndexOf(array.Max());
This works: 这有效:
var list = new [] {1,3,5,2,7,2};
var indices =
list
.Select((n, i) => new { n, i })
.OrderByDescending(x => x.n)
.Select(x => x.i)
.ToArray();
You can use Select
:- 您可以使用
Select
: -
var result = numbers.Select((v, i) => new { Value = v, Index = i })
.OrderByDescending(x => x.Value)
.Select(x => x.Index).ToArray();
Working Fiddle . 工作小提琴 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.