c# - 对双精度数组进行排序并返回原始索引的排序数组
问题描述
我想对一个双精度数组进行排序,但我想保存原始索引。我已经尝试了以下 -
double[] circs = new double[noOfCircs];
用值初始化 circs...
int[] loc = Enumerable.Range(0, noOfCircs-1).ToArray();
Array.Sort(circs, loc);
排序后,我想使用数组 loc 进行进一步的计算。我究竟做错了什么?
解决方案
我建议在匿名类的帮助下使用Linq:
using System.Linq;
...
double[] circs = ...
int[] loc = circs
.Select((value, index) => new { // for each item we store
value = value, // its value
index = index // and original index
})
.OrderBy(pair => pair.value) // Order by values
.Select(pair => pair.index) // By return original index
.ToArray();
推荐阅读
- powershell - Visual Studio 2019 Powershell 中的 Enter-VsDevShell 给出错误 FileNotFoundException
- ios - 如果 Swift 中有两个不同的类符合 CBCentralManagerDelegate 或 CBPeripheralManagerDelegate 协议,这是否有意义?
- python - 在 Python 中,在从另一个线程读取变量时从一个线程为变量分配一个新值是否安全?
- javascript - select2: items added programatically have no text
- python - Python Checking the beginning of a line for an ip address
- python - 等待dynamodb创建表,避免Requested resource not found
- python - My list prints out the Objects instead of the values
- python-3.x - Reshaping 1-D Data to 2-D Data for Matplotlib Pcolormesh
- php - PHP parse array to get index wise result
- python - Scrapy - multiple spiders - processing data from one spider while other are still running