Search code examples
c#listsortingordinal

Determine the ordinal position of elements in a list


I have a list of randomly generated numbers (let's call it numbers). I have another list of int of the same number of elements (I'll call it positions). The purpose of positions is to store the ordinal positions of the element. How do I go about this?

This is what I have thought about it:

  1. Creating a copy of numbers called copy
  2. Having to link it to numbers (sort of)
  3. Sorting copy
  4. Getting the indices of copy
  5. Storing the associated indices in positions

The second step is what I don't know how to go about.

I'm not sure if what I have thought is the best. So I'll actually prefer a better implementation of this.


Solution

  • Follow these steps:

    1. Create Pairs of int, for Example Tuple<int,int>
    2. For each element in numbers create a pair with the number and an increasing index
    3. Sort your pairs with the first number as a key
    4. Now you'll find the positions in the second int of your sorted pairs