如何排序在C#中的二维数组 [英] How to Sort 2D Array in C#
问题描述
我读过很多有关排序的二维数组的职位,但我仍然不能掌握它,所以我想知道是否有人能提供我一些意见...
我列出的字母和数量(我做一块文本的频率曲线变化分析)的阿雷。我读过这个数据到一个矩形阵列,并需要通过频率最高的第一个订购。这里是我的code迄今:
//创建二维数组包含ASCII code和数量
INT [,] letterFrequency =新INT [26,2]; //在二维数组填充ascaii code和数量
而(asciiNo< = 90)
{ 而((encryptedText.Length - 1)>计数器)
{
如果(asciiNo ==(INT)encryptedText [指数])
{
letterCount ++;
}
反++;
指数++;
} letterFrequency [(STORECOUNT),(0)] =(char)的(STORECOUNT + 66);
letterFrequency [(STORECOUNT),(1)] = letterCount;
STORECOUNT ++;
计数器= 0;
索引= 0;
letterCount = 0;
asciiNo ++;
}
您正在使用一个二维数组来重新present 2个独立的载体 - 符号和计数。相反,使用2个独立的数组。拥有的Array.Sort过载是发生在有一个阵列2阵列和排序,但将更改应用于两个,实现你想要什么。
这也将允许您使用一个char []的人物,而不是INT []:
的char []符号= ...
INT [] =计...
...加载数据...
的Array.Sort(计数,符号);
// 全做完了!
目前
点,计数已订购,而符号将仍然匹配指数按指数与它们涉及到的数量。
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++;
}
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.
这篇关于如何排序在C#中的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!