使用数字对字符串列表进行排序

编程入门 行业动态 更新时间:2024-10-19 12:39:06
本文介绍了使用数字对字符串列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试按数字顺序排序一些字符串,我尝试了一个排序列表框,甚至使用带数组的sort方法。我有三个字符串列表,并将它们组合成一个排序列表框。字符串的开头以这种格式的数字开头(1.id:字符串的名称),但数字不能正确排序,因为它将从20开始,因为它从20开始。我希望最大的数字首先向下。希望我解释这是正确的,因为这是一个非常奇怪的问题。 感谢您的帮助

I''m trying to sort some strings in order by number, I''ve tried a sorted listbox and even using the sort method with an array. I have three lists of strings and combine them into a sorted listbox. The beginning of the string starts with a number in this format (1.id:Name of string), but the numbers don''t sort properly as 20 will become before 120 because it starts with 20. I want the biggest number first downwards. Hope I explained this right as it''s a pretty weird problem. Thanks for any help

推荐答案

不要只使用排序,使用排序(比较< t>)< / t> : msdn.microsoft/en-us/library/w56d4y5z .aspx [ ^ ] 。 我希望你能找到比较标准:提取数字,使用 int.Parse (或一些其他合适的Parse)来找到数字,比较数字,而不是字符串。 然而,首先,想想你怎么会最终得到数字的字符串而不是具有适当结构化数据的列表或其他集合。这是目前最糟糕的初学者趋势之一:使用表示数据的字符串而不是数据本身。摆脱坏习惯。
-SA
Don''t use just Sort, use Sort(Comparison<t>)</t>: msdn.microsoft/en-us/library/w56d4y5z.aspx[^]. I hope, you can find the comparison criterion: extract the number, use int.Parse (or some other appropriate "Parse") to find the number, compare numbers, not strings. However, first of all, think how could you end up having strings with numbers, instead of a list or other collection with properly structured data. This is one of the worst beginner''s trends these days: using strings representing data instead of data itself. Get rid of the bad habits.
—SA

更多推荐

使用数字对字符串列表进行排序

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

发布评论

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

>www.elefans.com

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