数组排序,同时保留原索引
var arr = new[] { 5, 4, 2, 3, 1 };
var len = arr.Length;
var idx = new int[len];
for (int i = 0; i < len; i++)
{
idx[i] = i;
}
Array.Sort(idx, (m, n) =>
{
var diff = arr[m] - arr[n];
if (diff != 0) return diff;
return m - n;//如果一样大,索引小的在前面,保证稳定
});