使用嵌套环和子字符串方法(C#)排序的数组
我正在尝试使用以数字结尾的字符串输入手动对数组进行排序,我想从最高到最低排序。
例如,我可以从以下输出开始: ,姓名1:1540 ,名称2:2660 ,姓名3:80 ,Name4: 380
最后它应该是这样的: ,名称2:2660 ,姓名1:1540 ,姓名4:380 ,Name3: 80
private string[] OrderHighToLow(string[] data)
{
string temp;
for (int i = 0; i < data.Length; i++)
{
for (int y = 0; y < i; y++)
{
if (int.Parse(data[y].Substring((data[y].IndexOf(':') + 2))) > int.Parse(data[i].Substring((data[i].IndexOf(':') + 2))))
{
temp = data[i];
data[i] = data[y];
data[y] = temp;
}
}
}
return data;
}
这是我测试过的。根据我的说法,这应该可行,但关键是它不行,应用程序只是崩溃了。因此,如果这里有人能弄清楚为什么会这样,我将非常感激。提前致谢。
I am trying to manually sort an array using string inputs that end with a number which I want to sort from highest to lowest.
For example I can start with this output:
,Name1: 1540
,Name2: 2660
,Name3: 80
,Name4: 380
And in the end it should look like this:
,Name2: 2660
,Name1: 1540
,Name4: 380
,Name3: 80
private string[] OrderHighToLow(string[] data)
{
string temp;
for (int i = 0; i < data.Length; i++)
{
for (int y = 0; y < i; y++)
{
if (int.Parse(data[y].Substring((data[y].IndexOf(':') + 2))) > int.Parse(data[i].Substring((data[i].IndexOf(':') + 2))))
{
temp = data[i];
data[i] = data[y];
data[y] = temp;
}
}
}
return data;
}
This is what I have tested. According to me, this should work, but the point is it doesn't, the application just crashes. So, if anyone here can figure out why that may be, I would be very thankful. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的比较结果小于
不大于
your comparison is worng
less than Not greater than
这应该在
system.linq
的帮助下完成工作:希望这会有所帮助。
This should do the job with the Help of
System.Linq
:Hope this helps.