用另一个数组中的数字顺序制作一个数组

用另一个数组中的数字顺序制作一个数组

问题描述:

我正在考虑编写一个数组,该数组接受另一个数组的值,并根据它们的大小将它们分类"到另一个数组中.

I'm looking at writing an array that takes the values of another array and "sorts" them into another array based on their size.

示例:

[16、5、23、1、19]的数组

an array of [16, 5, 23, 1, 19]

最终会以

[2、1、4、0、3]

[2, 1, 4, 0, 3]

第一个数组可以是任意大小,但是假定其中没有任何重复的数字.它不应该按最大到最大的顺序对数字进行排序,保持数组中的位置至关重要.

The first array can be any size, but is assumed to not have any duplicate numbers in it. It should NOT sort the numbers by greatest to largest, maintaining position in the array is vital.

天真的实现:

var array = new []{16, 5, 23, 1, 19};

var sortedArray = array.OrderBy(x=>x).ToArray();

var result = new int[array.Length];

for(int i = 0; i<result.Length; i++)
    result[i] = Array.IndexOf(sortedArray, array[i]);