Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

How can i order a matrix?

$
0
0
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:

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];
            }


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!!!



Viewing all articles
Browse latest Browse all 2703

Trending Articles