【发布时间】:2015-06-11 04:58:56
【问题描述】:
我有一个 c# 程序,它显示你输入的 10 个数字中的最大数字。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Ole
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
for (int i = 0; i < 10; i++)
{
list.Add(Console.ReadLine());
}
list.Sort();
string Max = (string)list[list.Count - 1];
Console.WriteLine("{0}", Max);
Console.ReadLine();
}
}
}
但是,命令 list.Sort() 仅按第一个数字对其进行排序,例如:
24444
1212
2222
555
11
会是这样的:
11
1212
2222
24444
555
如何按所有数字对列表进行排序以获得“真实”的最高数字?
【问题讨论】:
-
How can I sort list by all digits to get "real" highest number??不要使用ArrayList。使用List<int>解析Console.ReadLine为int,然后将其存储在泛型类型安全列表中。 -
如果您真的需要以字符串前导类型对这些字符串进行排序,则使用零。取最大字符 24444 的长度,将其余字符更改为 00011、01212、00055... 等等。它们将按照编号进行排序。(或用空格引导它们)