0

I am currently working on a simulation system so i've an array like

int[] arr = {2,5,9,10,0, 4,1,5,3};

I want array of indexes of values based on lower and output result like

result = {4, 6, 0, 8, 1, 7, 2, 3};

I searched all over for almost 3 days i can't find.

2
  • Can you explain exactly how you want the results to be sorted? Commented Jul 30, 2022 at 12:24
  • Not sorted i just want the indexes based on lowest values eg in the above result array you can see, 4 is index of 0, 6 is index of 1, 0 is index of 2, 8 is index of 3 and etc. Commented Jul 30, 2022 at 12:34

2 Answers 2

2

In the 2nd array you want the indexes of the elements in the first array as sorted in ascending order.

You can use LINQ to do this

int[] arr = { 2, 5, 9, 10, 0, 4, 1, 5, 3 };
int[] result = arr.Select((x, i) => (x, i))
    .OrderBy(t => t.x)
    .Select(t => t.i)
    .ToArray();

Here, we used an overload of Select that yields the index: Select<TSource,TResult>(IEnumerable, Func<TSource,Int32,TResult>).

The first Select creates a ValueTuple.

The test

Console.WriteLine(String.Join(", ", result));

Yields the result:

4, 6, 0, 8, 5, 1, 7, 2, 3

Note that the number 5 appears twice in the input array. Therefore, the result is ambiguous. (Your expected result has only 8 indexes but the input array has a length of 9)


My full .NET 6.0 test code (Console App):

namespace CoreConsoleApp;

internal static class SortedArrayIndexes
{
    public static void Test()
    {
        //  Indexes   0  1  2   3  4  5  6  7  8
        int[] arr = { 2, 5, 9, 10, 0, 4, 1, 5, 3 };
        int[] result = arr.Select((x, i) => (x, i))
            .OrderBy(t => t.x)
            .Select(t => t.i)
            .ToArray();
        Console.WriteLine(String.Join(", ", result));
    }
}

It is called in my Main method with:

SortedArrayIndexes.Test();
Console.ReadKey();

In case you are working with an older Framework or language version, you can also use the older System.Tuple Class

int[] result = arr.Select((x, i) => new Tuple<int, int>(x, i))
    .OrderBy(t => t.Item1)
    .Select(t => t.Item2)
    .ToArray();
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much. Let me try it. As for duplicate i want it to be on five come first serve. That's get the first 5 index before the latter.
The documentation of Enumerable.OrderBy Method says in the remarks section: "This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved.". Therefore, "first come first serve" is guaranteed.
It's given an error on arr.Select((x, i) => (x, i)) a local variable x cannot be declared in this scope Another error in last ) expected
I don't know your full code, but you might have declared another variable x somewhere. Then replace x by something else (e.g. with v for value`). Also if you are using an older version of C# (<= 6.0), the ValueTuples will not work. Click on the ValueTuple link for more info. I added my full .NET 6.0 test code which is working.
Oh yes, mine is .NET 4.6
|
0

We can use the built-in Array.Sort() method. It has a second parameter that returns the sorted indices as well, but it requires us to make a new array with the indices (i.e. starts from 0 and ends at the length of the original array)

int[] arr = {2,5,9,10,0,4,1,5,3};
int[] indexArr = Enumerable.Range(0, arr.Length).ToArray();
Array.Sort(arr, indexArr);

Note that you will need to add using System.Linq; if you get an error that Enumerable doesn't exist.

1 Comment

I am not looking for sorting, i just want to get indexes of the values based on lowest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.