简体   繁体   中英

Sort a 2d Array of integers in C#

I want to manually sort a 2D Array in C#. I am confused on what to do when the pointer is on index 0 and I need to go to the previous row. My problem when I reach to index 0 I cant go any further and it is then out of bounds.

int temp3 = 25;
        bool swap = false;
        bool swap2 = false;
        long pointer = productarray[4,4];
        for(int i = 4; i >= 0; i--){

            for(int j = 4; j >= 0; j--){
                int temp5 = 1;
                while(swap != true){

                    if(temp3 != temp5){
                        pointer = productarray[i,j];

                            if (pointer < productarray [i, j - temp5]) {
                                long temporary = productarray [i, j];
                                productarray [i, j] = productarray [i, j - temp5];
                                productarray [i, j - temp5] = temporary;
                                temp5 = 1;


                            } else {
                                temp5++;

                            }

                    }
                    else{
                        swap = true; //Current pointer is the greatest int
                        temp3--;
                    }
                }
                swap = false;

            }
        }

Something like this

var myOrderedRows = myArray.OrderBy(row => row[columnIndex]);

from How can I sort a 2d array using Linq?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM