C# 使用浮点数组对整数数组进行排序
我试图对一个浮点数数组进行排序,但不知何故,要跟踪每个浮点数在数组中的原始位置。 我试图通过创建一个int数组来实现这一点,其中array[I]=I,然后根据浮点数组对该int数组进行排序 Array.Sort(Array,Array)看起来像是一种方法,所以我这样调用该方法:C# 使用浮点数组对整数数组进行排序,c#,arrays,sorting,runtime,C#,Arrays,Sorting,Runtime,我试图对一个浮点数数组进行排序,但不知何故,要跟踪每个浮点数在数组中的原始位置。 我试图通过创建一个int数组来实现这一点,其中array[I]=I,然后根据浮点数组对该int数组进行排序 Array.Sort(Array,Array)看起来像是一种方法,所以我这样调用该方法: Array.Sort(ints, floats); 但我得到一个运行时错误: ArgumentException: Value does not fall within the expected range. Syst
Array.Sort(ints, floats);
但我得到一个运行时错误:
ArgumentException: Value does not fall within the expected range.
System.Array.Sort[Int32,Single] (System.Int32[] keys, System.Single[] items, Int32 index, Int32 length, IComparer`1 comparer)
我知道这个方法适用于两个int数组
否则,是否有更好的方法对数组进行排序,但跟踪每个项目的原始位置
非常感谢 如果需要基于浮点值的原始数组中的浮点位置,请尝试使用字典
float[] f = new float[] { 1.4f, 2.4f, 1.9f };
Dictionary<float, int> origPos = new Dictionary<float, int>();
for (int i = 0; i < f.Length; i++)
{
origPos[f[i]] = i;
}
Array.Sort(f);
int pos = origPos[f[1]]; // this way you can get the position of a float in the original array
float[]f=新的float[]{1.4f,2.4f,1.9f};
字典origPos=新字典();
对于(int i=0;i
请您出示您的整数和浮点数
好吗?有很多类似的问题。。。请注意,最正确的方法可能是包含所有必要属性(索引、值等)的类,而不是尝试将多个数组排序在一起。在排序之前复制数组做你需要的事情?这会更简单,你只想知道每个项目的原始位置。我想这取决于具体情况。