C# 排序列表,逻辑比较
我有以下问题 我有一个包含字符串的列表,例如 (100_1, 100_2 ...., 100_10)
我用以下代码对列表进行排序,
extraImgsRaw.Sort((photo1, photo2) => photo1.CompareTo(photo2));
结果是: 100_1, 100_10, 100_2, 100_3 等等,
我想要的结果是一个逻辑比较 100_1、100_2,然后是 100_10,所以我更喜欢 自然数字排序不是字母排序。 我是否需要编写自己的比较类来实现 ICompare 接口,或者 LINQ 中有一个构建方法可以做到这一点?
先感谢您
i have the following problem
I have a list with strings for example (100_1, 100_2 .... , 100_10)
I sort the list with following code
extraImgsRaw.Sort((photo1, photo2) => photo1.CompareTo(photo2));
the result of this is : 100_1, 100_10, 100_2, 100_3 and so on
the result that I want is a logical compare like 100_1, 100_2 and then 100_10 so I prefer a
Natural numeric sort not a Alphabetic sort.
Do I need to write my own compare class that implements the ICompare interface or there is a build method in LINQ that does that?
thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有任何内置内容,但如果数据与您的问题中所示完全相同,那么敲出
Comparison
为您执行此操作:There's nothing built-in, but if the data is exactly as shown in your question then it shouldn't be too difficult to knock up a
Comparison<T>
to do this for you:拆分和比较元素,
这是我为“版本”编写的一个。
Split and compare elements,
Here is one I wrote for 'versions'.