# Thread: Sorting elements in 2d array by their index

1. ## Sorting elements in 2d array by their index

Hi!

I need to sort the elements in a 2d array by their index (starting from 1) for example:

Code:
```1  5  3
4  7  8
4 10  2```
10 is the biggest element and its index is 32, after 10 comes 8 with index 23 etc etc... Please help?
EDIT: If you can give me examples for two orders ... By descending and ascending order... Thanks.

2. your description isn't clear. what do you mean buy 'index'? if each element has a value and an index, your example should show that. and it would help to show the initial state and the final sorted state that you are looking for.

3. For the given matrix in my example number 1 is on position 11 number ,5 - position 12 etc etc, so... i need to sort the elements in this style:

32 (that's 10), 23(that's 8), 22 (that's 7) ... etc etc until it comes to the smallest element. Understand now?

4. nope

5. You need to get used to the idea that arrays in C/C++ are zero based.

Next you have a starting matrix of:
Code:
```1  5  3
4  7  8
4 10  2```
Which you wish to sort with the result being:
Code:
```10 8  7
5  4  4
3  2  1```
Is that correct?

What have you tried?

Jim

6. It seems the OP wants to output the (1-based) indices of the matrix elements in order of the element value. So this input:
Code:
```1  5  3
4  7  8
4 10  2```
would give this output (assuming descending order is wanted) :
Code:
```32  (row number, column number)
23
22
12
21  (not sure how to handle equal ...)
31  (... element values (both these are 4))
13
33
11```