我有下面的示例代碼:的Array.Sort與數字的字符串
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"
只是spitballing,但如果你想要「Hello1」來之前「Hello10」,你最好注入一個0,所以它會是「Hello01」,「Hello02」等。Windows資源管理器排序文件和文件夾在相同時尚,按字母順序完成。可能要查看字符串的最後2個字符,並確定它們是否都是**數字,如果不是,則以0爲前導。 – sab669
@ sab669我不允許更改原始數據,它們非常重要對整個應用程序敏感。 –