简体   繁体   中英

C# Sort 2D array based on key array

I'm trying to sort 2D array of chars based on 1D array as key. I need to find a char that wasn't used in key so far and then sort it based on it.
Fe:

AEITY
EDNTB

to

TEAIY
TDENB

based on key:

TEAIY

on row 0

My code struggle with an multiple chars and provide bad results:
Fe:

AAABN
TEAIY
TDENB

to

ABANA
TIEYA
TNDBE

but instead of that i get

ABANA
AIEYT
ENDBT

based on key:

ABANA

Code i figure out:

static char[][] SortSimiliarity(char[][] arr, char [] key, int arrRow)
    {

        Dictionary<char, int> dcKeyList = new Dictionary<char, int>();

        for (var i = 0; i < key.Length; i++)
        {
            var counterCurrentKey = 0;
            for (var j = 0; j < key.Length; j++)
            {
                if(key[i] == arr[arrRow][j]) 
                {
                    //Console.WriteLine("{0} == {1}", key[i], arr[arrRow][j]);

                    if (!dcKeyList.ContainsKey(key[i]))
                    {
                        for (var k = 0; k < key.Length; k++)
                        {
                            //Console.WriteLine("x");
                            var temp = arr[k][j];
                            arr[k][j] = arr[k][i];
                            arr[k][i] = temp;
                        }
                        dcKeyList.Add(key[i], 1);
                    }
                    else
                    {   
                        if (dcKeyList[key[i]] == counterCurrentKey)
                        {
                            Console.WriteLine("key is {0}", dcKeyList[key[i]]);
                            for (var k = 0; k < key.Length; k++)
                            {
                                //Console.WriteLine("x");

                                var temp = arr[k][j];
                                arr[k][j] = arr[k][i];
                                arr[k][i] = temp;
                            }
                            dcKeyList[key[i]]++;
                        }
                        counterCurrentKey++;
                    }
                }              
            }

        }
        return arr;
    }

I know there is something wrong with that ELSE statement, where i compare current count of char in key loop. Thank you for your ideas.

My problem was simple. I tried to sort 2d array based on array key which i does, but i also save same result into same 2d array, without keeping tracks on changes. That can be solved with pointer logic or with new array where you copy only your sorted results without changing the base 2D array.

 static char[][] SortSimiliarity(char[][] arr, char[] key, int arrRow)
            {
            // init dict

            Dictionary<char, int> dict = new Dictionary<char, int>();

            // init new matrix
            char[][] tempArray = new char[5][];
            for(var i = 0; i < 5; i++)
            {
                tempArray[i] = new char[5];
            }

            // copy new sorted keys
            for (var i = 0; i < key.Length; i++)
            {
                for (var j = 0; j < key.Length; j++)
                {
                    if (key[i] == arr[arrRow][j])
                    {
                        if (!dict.ContainsKey(key[i]))
                        {
                            dict.Add(key[i], j);
                            for(var k = 0; k < key.Length; k++)
                            {
                                tempArray[k][i] = arr[k][j];
                            }
                            break;
                        }
                        else
                        {
                            if(j != dict[key[i]])
                            {
                                for (var k = 0; k < key.Length; k++)
                                {
                                    tempArray[k][i] = arr[k][j];
                                }
                                dict[key[i]] = j+1;
                            }
                        }
                    }
                }
            }
            return tempArray;
        }

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