#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# How can i order a matrix?

Posts: 1Member
in C#
Hi guys, i've this matrix:

3 1
10 1
30 1
41 1
10 4
21 4
30 4
10 7
10 10
30 10

i need to sort from the first coloumn.
This is what i should be obtained

3 1
10 1
10 4
10 7
10 10
21 4
30 1
30 4
30 10
41 1

i wrote this code:

[code]int[,] id = matrixPlusSort;
int[,] idcol = new int[matrixPlusSort.GetLength(0), matrixPlusSort.GetLength(1)];
int a;
int b;
int d;
for (int i = 0; i < matrixPlus.GetLength(0); i++)
{

a = id[i, 0];
d = id[i, 1];
b = i;
int b1 = i;
while ((b > 0) && (id[b - 1, 0] > a))
{
id[b, 0] = id[b - 1, 0];
b = b - 1;
}
id[b, 0] = a;
idcol[b, 1] = d;
idcol[b1, 1] = d;
}
int[,] id3 = new int[matrixPlusSort.GetLength(0), matrixPlusSort.GetLength(1)];
for (int i = 0; i < matrixPlus.GetLength(0); i++)
{
id3[i, 0] = id[i, 0];
id3[i, 1] = idcol[i, 1];
}[/code]

With this code my output is:

3 1
10 1
10 4
10 7
10 10
21 4
30 4 *
30 7 *
30 10
41 10*

I marked with * rows that have the wrong value in the second column.

Can you help me understand what am I doing wrong?

Thanks!!!

· ·