如何在 C# 中对二维数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8866414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Sort 2D Array in C#
提问by L1am22
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++;
}
采纳答案by Marc Gravell
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 onearray, but applies the changes to both, achieving what you want.
您正在使用二维数组来表示 2 个单独的向量 - 符号和计数。相反,使用 2 个单独的数组。Array.Sort 有一个重载,它接受 2 个数组,并对一个数组进行排序,但将更改应用于两者,实现您想要的。
This would also allow you to use a char[] for the characters rather than int[]:
这也将允许您对字符使用 char[] 而不是 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.
此时,计数已排序,并且符号仍将逐个索引与它们相关的计数匹配。
回答by Aidiakapi
In this case I'd choose to make use of KeyValuePair<TKey, TValue>and instead use something like this:
在这种情况下,我会选择使用KeyValuePair<TKey, TValue>而不是使用这样的东西:
//create 2D array to contain ascii code and quantities
KeyValuePair<char, int>[] letterFrequency = new KeyValuePair<char, int>[26];
//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] = new KeyValuePair<char, int>((char)(storeCount+66), letterCount);
storeCount++;
counter=0;
index=0;
letterCount = 0;
asciiNo++;
}
Then use Array.Sort:
然后使用Array.Sort:
Array.Sort(letterFrequency, (i1, i2) => i2.Value.CompareTo(i1.Value));
回答by Marc Gravell
Alternative approach:
替代方法:
var counts = new Dictionary<char,int>();
foreach(char c in text) {
int count;
counts.TryGetValue(c, out count);
counts[c] = count + 1;
}
var sorted = counts.OrderByDescending(kvp => kvp.Value).ToArray();
foreach(var pair in sorted) {
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
(untested)
(未经测试)
回答by Dmitry Polyanitsa
You can wrap letter-count pair in a struct and use linq methods to manipulate data:
您可以将字母计数对包装在结构中并使用 linq 方法来操作数据:
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();
回答by Aidiakapi
This'll sort a two dimension array, the bool specifies if it's sorted on the second dimension, but default it sorts on the first dimension.
这将对二维数组进行排序,bool 指定它是否在第二维上排序,但默认情况下它在第一维上排序。
void SortDoubleDimension<T>(T[,] array, bool bySecond = false)
{
int length = array.GetLength(0);
T[] dim1 = new T[length];
T[] dim2 = new T[length];
for (int i = 0; i < length; i++)
{
dim1[i] = array[i, 0];
dim2[i] = array[i, 1];
}
if (bySecond) Array.Sort(dim2, dim1);
else Array.Sort(dim1, dim2);
for (int i = 0; i < length; i++)
{
array[i, 0] = dim1[i];
array[i, 1] = dim2[i];
}
}
回答by Olivier Jacot-Descombes
Why are you storing the character? You can infer it from the array index and do not need to store it! Use a one-dimensional array instead.
为什么要存储角色?您可以从数组索引中推断出来,不需要存储它!改用一维数组。
string encryptedText = "Test".ToUpper();
int[] frequency = new int[26];
foreach (char ch in encryptedText) {
int charCode = ch - 'A';
frequency[charCode]++;
}
var query = frequency
.Select((count, index) => new { Letter = (char)(index + 'A'), Count = count })
.Where(f => f.Count != 0)
.OrderByDescending(f => f.Count)
.ThenBy(f => f.Letter);
foreach (var f in query) {
Console.WriteLine("Frequency of {0} is {1}", f.Letter, f.Count);
}
回答by Raz Megrelidze
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++;
}
}
}

