如何根据大小对整数进行排序并通过变量名称跟踪它们的位置
我有一个包含多个 int 变量的程序,每次遇到设置的失败条件时,都会将单独的计数添加到特定变量中。我希望用户能够通过单击按钮来跟踪他们遇到的每个类别的失败次数。我想按从最高值整数到最低值整数的顺序在 datagridview 上显示范围。我还需要在相邻列中显示与该值相关的测试步骤的名称。我的计划是使用 Array.sort 对整数进行排序,但随后我忘记了它们的名称,因此无法分配相邻的字符串列。我尝试使用哈希表,但如果我使用字符串作为键,它会按字母顺序而不是数字排序,如果我使用整数作为键,我会得到重复的条目,这些条目不会添加到哈希表中。这是我尝试过的一些示例,但它们存在上述问题。本质上我想以两个数组结束,其中顺序与命名和值约定匹配。仅供参考,变量是在这部分代码之前声明的,以 x 结尾的变量是整数(非 x)值的字符串名称。
Hashtable sorter = new Hashtable();
sorter[download] = downloadx;
sorter[power] = powerx;
sorter[phase] = phasex;
sorter[eeprom] = eepromx;
sorter[upulse] = upulsex;
sorter[vpulse] = vpulsex;
sorter[wpulse] = wpulsex;
sorter[volts] = voltsx;
sorter[current] = currentx;
sorter[ad] = adx;
sorter[comms] = commsx;
sorter[ntc] = ntcx;
sorter[prt] = prtx;
string list = "";
string[] names = new string[13];
foreach (DictionaryEntry child in sorter)
{
list += child.Value.ToString() + "z";
}
int[] ordered = new int[] { download, power, phase, eeprom, upulse, vpulse, wpulse, volts, current, ad, comms, ntc, prt };
Array.Sort(ordered);
Array.Reverse(ordered);
for (int i = 0; i < sorter.Count; i++)
{
int pos = list.IndexOf("z");
names[i] = list.Substring(0, pos);
list = list.Substring(pos + 1);
}
这是第一个问题,希望它不要太冗长。
谢谢
I have a program with multiple int variables where individual counts are added to the specific variable each time a set fail condition is encountered. I want the user to be able to track how many failures of each category they have encountered by a button click. I want to display the range on a datagridview in order from highest value integer down to lowest. I also need to display in the adjacent column the name of the test step that relates to the value. My plan was to use Array.sort to order the integers but i then lose track of their names so cant assign the adjacent string column. I tried using a hashtable but if i use the string as a key it sorts alphabetically not numerically and if i use the integer as a key i get duplicate entries which dont get added to the hash table. here is some of the examples i tried but they have the aforementioned problems. essentially i want to end with two arrays where the order matches the naming and value convention. FYI the variables were declared before this section of code, variables ending in x are the string name for the (non x) value of the integer.
Hashtable sorter = new Hashtable();
sorter[download] = downloadx;
sorter[power] = powerx;
sorter[phase] = phasex;
sorter[eeprom] = eepromx;
sorter[upulse] = upulsex;
sorter[vpulse] = vpulsex;
sorter[wpulse] = wpulsex;
sorter[volts] = voltsx;
sorter[current] = currentx;
sorter[ad] = adx;
sorter[comms] = commsx;
sorter[ntc] = ntcx;
sorter[prt] = prtx;
string list = "";
string[] names = new string[13];
foreach (DictionaryEntry child in sorter)
{
list += child.Value.ToString() + "z";
}
int[] ordered = new int[] { download, power, phase, eeprom, upulse, vpulse, wpulse, volts, current, ad, comms, ntc, prt };
Array.Sort(ordered);
Array.Reverse(ordered);
for (int i = 0; i < sorter.Count; i++)
{
int pos = list.IndexOf("z");
names[i] = list.Substring(0, pos);
list = list.Substring(pos + 1);
}
First question here so hope its not too longwinded.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用字典。您可以按值排序:myDico.OrderBy(x => x.Value).Reverse(),排序将按数字降序排列。您只需枚举结果即可。
我希望我理解你的需要。不然不理我。
Use a Dictionary. And you can order it by the value : myDico.OrderBy(x => x.Value).Reverse(), the sort will be numerical descending. You just have to enumerate the result.
I hope I understand your need. Otherwise ignore me.
你想使用 a
来存储你的数字。我不清楚你最后如何显示结果 - 你有网格或列表控件吗?
你问的是用途。您已经拥有哪些?
.NET 2.0 的编辑
可能有一个更优雅的解决方案,但您可以通过将行放入 DataTable 来实现逻辑。然后,您可以创建该表的 DataView 并按您喜欢的任何列(升序或降序)排序。
请参阅 http://msdn.microsoft例如.com/en-us/library/system.data.dataview(v=VS.80).aspx。
.NET 3.5 及更高版本的 EDIT
至于按字典值排序:
如果您需要结果是字典,则可以对其调用
.ToDictionary()
。对于倒序,请使用.OrderByDescending(pair =>pair.Value)
。You want to be using a
to store your numbers.I'm not clear on how you're displaying results at the end - do you have a grid or a list control?
You ask about usings. Which ones do you already have?
EDIT for .NET 2.0
There might be a more elegant solution, but you could implement the logic by putting your rows in a DataTable. Then you can make a DataView of that table and sort by whichever column you like, ascending or descending.
See http://msdn.microsoft.com/en-us/library/system.data.dataview(v=VS.80).aspx for example.
EDIT for .NET 3.5 and higher
As far as sorting a Dictionary by its values:
If you need the results to be a Dictionary, you can call
.ToDictionary()
on that. For reverse order, use.OrderByDescending(pair => pair.Value)
.