How to Sort 2D Array in C#
Solution 1
You are using a 2D array to represent 2 separate vectors - the symbols and the counts. Instead, use 2 separate arrays. Array.Sort has an overload that takes 2 arrays, and sorts on one array, but applies the changes to both, achieving what you want.
This would also allow you to use a char[] for the characters rather than int[]:
char[] symbols = ...
int[] counts = ...
...load the data...
Array.Sort(counts, symbols);
// all done!
At this point, the counts have been ordered, and the symbols will still match index-by-index with the count they relate to.
Solution 2
You can wrap letter-count pair in a struct and use linq methods to manipulate data:
struct LetterCount {
public char Letter { get; set; }
public int Count { get; set; }
}
Sorting by count will look like this:
List<LetterCount> counts = new List<LetterCount>();
//filling the counts
counts = counts.OrderBy(lc => lc.Count).ToList();
Solution 3
public static void Sort2DArray<T>(T[,] matrix)
{
var numb = new T[matrix.GetLength(0) * matrix.GetLength(1)];
int i = 0;
foreach (var n in matrix)
{
numb[i] = n;
i++;
}
Array.Sort(numb);
int k = 0;
for (i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
matrix[i, j] = numb[k];
k++;
}
}
}
L1am22
Updated on July 05, 2022Comments
-
L1am22 almost 2 years
I've read lots of posts about sorting a 2D array but I still can't master it so I was wondering if anyone can offer me some advice...
I have an aray which lists letters and quantity (I'm doing a frequency anaysis on a piece of text). I've read this data into a rectangle array and need to order it by highest frequency first. Here's my code so far:
//create 2D array to contain ascii code and quantities int[,] letterFrequency = new int[26, 2]; //fill in 2D array with ascaii code and quantities while (asciiNo <= 90) { while ((encryptedText.Length - 1) > counter) { if (asciiNo == (int)encryptedText[index]) { letterCount++; } counter++; index++; } letterFrequency[(storeCount), (0)] = (char)(storeCount+66); letterFrequency[(storeCount), (1)] = letterCount; storeCount++; counter=0; index=0; letterCount = 0; asciiNo++; }
-
Aidiakapi over 12 yearsBetter make use of a
SortedDictionary<TKey, TValue>
then. But your other solution is the best :P. -
Aidiakapi over 12 yearsThat's the same as using the generic
KeyValuePair<TKey, TValue>
, except you get to name it. -
Marc Gravell over 12 years@Aidiakapi no; that sorts on the key; we want to sort on the value
-
Dmitry Polyanitsa over 12 yearsYes, but naming gives more readability and making a struct now can prepare the code for new logic in the future.
-
Aidiakapi over 12 yearsSorry, I meant to say
SortedList<TKey, TValue>
;) -
Aidiakapi over 12 yearsImo it just clutters code, but like I say that's my opinion. Just like C++ with it's typedefs. If you have a zillion types that all refer to the same type. Then it only gets confusing. Once again, my opinion.
-
Dmitry Polyanitsa over 12 yearsIn this case it's a matter of taste, I agree.