【发布时间】:2014-04-01 20:44:27
【问题描述】:
我有以下示例代码:
List<string> test = new List<string>();
test.Add("Hello2");
test.Add("Hello1");
test.Add("Welcome2");
test.Add("World");
test.Add("Hello11");
test.Add("Hello10");
test.Add("Welcome0");
test.Add("World3");
test.Add("Hello100");
test.Add("Hello20");
test.Add("Hello3");
test.Sort();
但发生的情况是,test.Sort 会将数组排序为:
"Hello1",
"Hello10",
"Hello100",
"Hello11",
"Hello2",
"Hello20",
"Hello3",
"Welcome0",
"Welcome2",
"World",
"World3"
有没有办法对它们进行排序,以便string 也有正确的数字顺序?
(如果string 的末尾没有数字,那string 将始终排在最前面 - 在字母顺序之后)
预期输出:
"Hello1",
"Hello2",
"Hello3",
"Hello10",
"Hello11",
"Hello20",
"Hello100",
"Welcome0",
"Welcome2",
"World",
"World3"
【问题讨论】:
-
只是吐口水,但如果你想让 "Hello1" 出现在 "Hello10" 之前,你最好注入一个 0,所以它是 "Hello01"、"Hello02" 等。 Windows 资源管理器按字母顺序对文件和文件夹进行排序。可能想查看字符串的最后 2 个字符并确定它们是否是 both 数字,如果不是,则以 0 开头。
-
@sab669 我不允许更改原始数据,它们对整个应用程序非常敏感。
标签: c#