Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 30-Mar-13 18:37pm
Comments
Sergey Alexandrovich Kryukov at 30-Mar-13 23:58pm
   
This is simple, but... how come you got such list in first place?
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Don't use just Sort, use Sort(Comparison):
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
  Permalink  
Comments
Kieran Crown at 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 at 31-Mar-13 0:18am
   
Do you see that I answered your question in full? Use the said Sort method.
—SA
Kieran Crown at 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 at 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)

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 30 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100