用另一个数组中的数字顺序制作一个数组 [英] Making an array with the order of numbers from another array

查看:50
本文介绍了用另一个数组中的数字顺序制作一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]);

这篇关于用另一个数组中的数字顺序制作一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆