C#中的自然数值排序

编程入门 行业动态 更新时间:2024-10-18 16:44:11
本文介绍了C#中的自然数值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在C#中对通用列表进行排序时遇到问题

I have a problem when sorting a generic list in C#

我有一个List<MyObjects> myList,而MyObject有一个字符串属性.

I have a List<MyObjects> myList, and MyObject has a string property.

现在对降序排序时看起来像这样

Now it looks like this when sorting descending

2.4.88

2.4.88

2.4.70

2.4.164->这是错误的

2.4.164 -> this is wrong

2.4.15

如何对列表进行排序?

我尝试过:

myList.sort(delegate(MyObjects obj1, MyObjects obj2) { return obj2.version.CompareTo(obj1.version); });

使用Linq(旧框架)不是一种选择

Its not an option to use Linq (older framework)

更新:我的列表也可以包含N/A

推荐答案

您不能将其作为字符串进行比较,因为显然这就是正确的字符串排序方式.您需要解析为数字或 Version You cannot compare as strings, because obviously thats the proper string sorting. You need to parse to numbers or to an instance of the Version class:

myList.sort(delegate(MyObjects obj1, MyObjects obj2) { return new Version(obj2.version).CompareTo(new Version(obj1.version)); });

更多推荐

C#中的自然数值排序

本文发布于:2023-11-29 20:47:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数值   自然

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!