根据元素数组的数组排序和收益指数在C#中的大小 [英] Sort an array and return index of arrays according to elements size in c#

查看:150
本文介绍了根据元素数组的数组排序和收益指数在C#中的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从最小到最大的价值排序的数组,但我需要整理后只返回数组的索引。我不想交换价值,我只需要根据价值大小返回值指数,
对于如

I need to sort the array from minimum to maximum value, but I need to return only the index of array after sorting it. I dont want to swap values, I just need to return the values index according to the value size, for eg

int[] arr = {7,8,2,3,1,5};
for (int i=0; i<=arr.length; i++)
{
   int index = Array.IndexOf(arr, i);
}

现在我想从最小的返回值的指数最大
为4,2,3,5,0,1。

Now I want to return index of values from minimum to maximum as 4,2,3,5,0,1.

推荐答案

您在检查for循环是错了,它应该是 I&LT; arr.Length 。对于指数,你可以做:

Your check in the for loop is wrong it should be i < arr.Length. For index you can do:

int[] arr = { 7, 8, 2, 3, 1, 5 };
int[] sortedIndexArray = arr.Select((r, i) => new { Value = r, Index = i })
                            .OrderBy(t => t.Value)
                            .Select(p => p.Index)
                            .ToArray();

有关输出:

foreach(int item in sortedIndexArray)
    Console.WriteLine(item);

输出:

4
2
3
5
0
1

这篇关于根据元素数组的数组排序和收益指数在C#中的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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