Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 30-Mar-13 23:58pm    
This is simple, but... how come you got such list in first place?
—SA

1 solution

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
 
Share this answer
 
Comments
Kieran Crown 31-Mar-13 0:16am    
I'm basically downloading the data from a MySQL database. I need the name of the data (string) Nd to sort them strings by a number (popularity). I'm making my own custom auto suggest feature and need to sort the results. Thanks
Sergey Alexandrovich Kryukov 31-Mar-13 0:18am    
Do you see that I answered your question in full? Use the said Sort method.
—SA
Kieran Crown 31-Mar-13 0:46am    
Yes I did but you also asked me why I needed such a list so I was just answering that.
Sergey Alexandrovich Kryukov 31-Mar-13 1:05am    
Sure, I understand that... Thank you for answering.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900