Question Is Array.Sort() able to sort this (and save the indexes)?

vindiou

New member
Joined
Dec 20, 2019
Messages
3
Programming Experience
Beginner
Hi, I have a double 2D array:

a[0,0]=1.1 a[0,1]=0.1 a[0,2]=2.9 a[0,3]=1.6
a[1,0]=-2.2 a[1,1]=-1.7 a[1,2]=0.3 a[1,3]=-0.4
a[2,0]=2.0 a[2,1]=-0.1 a[2,2]=-1.8 a[2,3]=-3.1

1) I want to sort it in descending order AND save the 2 indexes (in order to know which array indexes has the absolute higher/lower values):

a[2,3]=-3.1
a[0,2]=2.9
a[1,0]=-2.2
a[2,0]=2.0
a[2,2]=-1.8
a[1,1]=-1.7
a[0,3]=1.6
a[0,0]=1.1
a[1,3]=-0.4
a[1,2]=0.3
a[0,1]=0.1
a[2,1]=-0.1

2) I also need another sort: on the first index and save the 2nd index:

a[0,2]=2.9
a[0,3]=1.6
a[0,0]=1.1
a[0,1]=0.1

a[1,0]=-2.2
a[1,1]=-1.7
a[1,3]=-0.4
a[1,2]=0.3

a[2,3]=-3.1
a[2,0]=2.0
a[2,2]=-1.8
a[2,1]=-0.1

Is it possible with "Array.Sort()" ? As a beginner, help will be greatly appreciated!

Thank you very much!
 
In addition to the answer to your original question answered by @JohnH above, it looks like aren't sure about what you are asking for.

For example in your first sort order, the following does quite look right:
1) I want to sort it in descending order AND save the 2 indexes (in order to know which array indexes has the absolute higher/lower values):

a[2,3]=-3.1
a[0,2]=2.9
a[1,0]=-2.2
a[2,0]=2.0
a[2,2]=-1.8
a[1,1]=-1.7
a[0,3]=1.6
a[0,0]=1.1
a[1,3]=-0.4
a[1,2]=0.3
a[0,1]=0.1
a[2,1]=-0.1
The value -3.1 is less that 2.9, yet somehow you say that is the correct descending order. Perhaps you mean descending absolute value order?

Anyway, the way to go about this is to create a list of classes or structs that look something like:
C#:
class Entry
{
    double Value;
    int Row;
    int Column;
}
and then sort by the absolute value of the Value member.
 
And then to perform the second sorting that you want, it would be simply grouping the list by the Row and then sorting again by the absolute value of the Value member within each group.
 
Looking at the data as you presented above and the types of sorting you are trying to do, it looks like you are trying to do some statistical analysis of 3 different groups, both within their groups, and as a combined population. Some of it looks like my wife's homework while she was doing a Statistics class for her Master's. So although statisticians like to present data in tabular form like you did above, be aware that modern programmers prefer to see things as objects, rather than rows and columns in a table. The typical object oriented view of tabular data is that columns are properties of objects and each row is an instance of an object.
 
Back
Top Bottom