使用数字对字符串列表进行排序 [英] Sorting a list of strings with numbers

查看:75
本文介绍了使用数字对字符串列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按数字顺序排序一些字符串,我尝试了一个排序列表框,甚至使用带数组的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>

http://msdn.microsoft.com/en-us/library/w56d4y5z .aspx [ ^ ] 。



我希望你能找到比较标准:提取数字,使用 int.Parse (或一些其他合适的Parse)来找到数字,比较数字,而不是字符串。



然而,首先,想想你怎么会最终得到数字的字符串而不是具有适当结构化数据的列表或其他集合。这是目前最糟糕的初学者趋势之一:使用表示数据的字符串而不是数据本身。摆脱坏习惯。



-SA
Don''t use just Sort, use Sort(Comparison<t>)</t>:
http://msdn.microsoft.com/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


这篇关于使用数字对字符串列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆