c# - 列表排序未产生预期结果
问题描述
我有一个似乎没有正确排序的列表。为了找出发生了什么,我添加了代码:
// save unsorted list
using (StreamWriter listfile = new StreamWriter("D:\\UnsortedList.csv",
false))
{
foreach (string currentLine in FileList)
{
listfile.WriteLine(currentLine);
}
listfile.Close();
}
然后我对其进行排序并将结果保存到另一个文件:-
FileList.Sort();
// save sorted list
using (StreamWriter listfile = new
StreamWriter("D:\\UnfilteredFileList.csv", false))
{
foreach (string currentLine in FileList)
{
listfile.WriteLine(currentLine);
}
listfile.Close();
}
第一个未排序的文件包含一些行(我添加的行号):
1 attributes\53.p_40NB MED 90º ELBOW,zipped
2 attributes\6.00 B02 level.PObjGrp,zipped
3 attributes\6.00- B02 level.PObjGrp,zipped
4 attributes\6.01- B02 level.PObjGrp,zipped
5 attributes\6.02- B03 level.PObjGrp,zipped
..........
6 attributes\53.p_40NB MED 90º ELBOW,folder
7 attributes\6.00 B02 level.PObjGrp,folder
8 attributes\6.00- B02 level.PObjGrp,folder
9 attributes\6.01- B02 level.PObjGrp,folder
10 attributes\6.02- B03 level.PObjGrp,folder
在排序之后,我预计第 7 行将在第 2 行之前结束。相反,我得到了:
attributes\53.p_40NB MED 90º ELBOW,zipped
attributes\6.00 B02 level.PObjGrp,folder
attributes\6.00- B02 level.PObjGrp,folder
attributes\6.00 B02 level.PObjGrp,zipped
attributes\6.00- B02 level.PObjGrp,zipped
attributes\6.01- B02 level.PObjGrp,folder
我误解了排序吗?
解决方案
内部字符串比较器逐字比较字符串,所以使用肯定比较器,如:
FileList.Sort(string.CompareOrdinal);
推荐阅读
- python - 如何使用类似于 Scikit 类权重的 Logistic Regression Tensorflow Estimator
- r - 创建一个函数,将 n <5 的值替换为 1 到 4 之间的随机数(整数)
- vba - GetObject 可以做到这一点吗?
- javascript - 如何从javascript中的数组中获取名称?
- javascript - 如何从无处提交按钮?
- angular - 如何在手风琴中保持至少一个面板展开?
- xml - Xpaths,根据其子项的值返回一个 ref 字段
- macos - 什么是 macOS Mojave 中的 CoreMediaIo DAL 虚拟相机替代品
- javascript - 我正在尝试将 Material-UI InfoIcon 添加到我的代码中。但我不知道如何在 TextField 中实现它
- cordova - 使用 Phaser-js 和 Cordova 开发的移动问答游戏