如何对段落列表进行排序
A 有一个列表:
§ 21 项
§ 1 项
§ 13 项目
§ 3 项
§ 2 项目
我需要这样订购:
§ 1 项
§ 2 项目
§ 3 项
§ 13 项目
§ 21 项目
但是在
newList = (from a in list orderby a.Name ascending select a).ToList();
我得到这个之后:
§ 1 项
§ 13 项目
§ 2 项目
§ 21 项
§ 3 项目
如何解决?
A have a list:
§ 21 Item
§ 1 Item
§ 13 Item
§ 3 Item
§ 2 Item
I need to order it like this:
§ 1 Item
§ 2 Item
§ 3 Item
§ 13 Item
§ 21 Item
But after
newList = (from a in list orderby a.Name ascending select a).ToList();
I get this:
§ 1 Item
§ 13 Item
§ 2 Item
§ 21 Item
§ 3 Item
How to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您正在寻找的是自然排序而不是默认的词法排序。实现自然排序的 C# 比较器可以在 http://www.davekoelle.com/alphanum.html< 找到/a>
What you are looking for is a natural sort rather than the default lexical sort. A C# comparer that implements natural sort can be found at http://www.davekoelle.com/alphanum.html
谷歌有答案:获取Linq to OrderBy 以数字表示的属性值。
Google has the answer: Getting Linq to OrderBy an attribute value numerically.
01
与1
不同。按字典顺序,13
位于3
之前。假设所有“段落”都以非零前缀整数开头,您将需要解析每个整数并按该整数对列表进行排序。如果列表项没有以整数为前缀,前面的答案将抛出
FormatException
。01
is different from1
. Lexicographically13
comes before3
. Presuming that all "paragraphs" are prepended with a non-zero-prefixed integer, you will need to parse each integer and order your list by that.The previous answers will throw
FormatException
if a list item is not prefixed by an integer.这应该有效:
This should work:
容纳空白条目
to accomodate blank entry