【C#】实现按Windows排序方式排序
很多人或许都遇到过类似的问题,C#读取的文件列表默认是按ASCII排序的,这样会出现一个问题就是10会排在2的前面。
那么是否可以解决呢,答案是肯定的。虽然这个是很早之前遇到的问题,这次突然想起来,就写一下,记录下。
解决方案:
[System.Runtime.InteropServices.DllImport("Shlwapi.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
private static extern int StrCmpLogicalW(string psz1, string psz2);
调用上述方法进行排序即可实现windows自带效果的排序了。
例:
List<string> list = new List<string>(); list.Add("10"); list.Add("2");
list.Add("12"); list.Sort(StrCmpLogicalW);
这样就完美解决了。