-
Nov 4th, 2009, 07:51 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] String-Number Sorting
I need to sort a list of strings, which contain numbers and letters.
When I perform a sort, I get patterns not dissimilar to those below:
Code:
1-A
10-A
11-A
...
19-A
2-A
20-A
21-A
Is there any quick way to fix this problem, or do I have to pad the numbers within the strings prior to sorting?
"Why do all my attempts at science end with me getting punched by batman?" xkcd.
| Pong| |
Sorry for not posting more often.
-
Nov 4th, 2009, 09:21 PM
#2
Re: String-Number Sorting
You just have to provide your own custom function to compare two items in the list. As it stands they are being compared using the standard String.CompareTo method, which is going to sort them alphanumerically. You need to provide your own function that will compare them in the way that you want, using either the Comparison delegate or the IComparer interface. Follow the Blog link in my signature and you'll find a three part submission dedicated to sorting lists. It's the first three posts I made, so look for the earliest dates.
-
Nov 5th, 2009, 08:15 PM
#3
Thread Starter
Hyperactive Member
Re: String-Number Sorting
Thanks jmc. That solved that problem!
I've got a similar problem in a datagridview now, as well. Often, a string column will be filled entirely with numbers (but still needs to have the occasional string value). When someone clicks the header, it needs to test for numbers rather than simply sorting alphanumerically.
In this case, I don't know where to implement the comparison function: the sorting is done automatically when the header is clicked. I've tried changing SortMode to Programmatic, but apart from preventing me from sorting at all that didn't help.
"Why do all my attempts at science end with me getting punched by batman?" xkcd.
| Pong| |
Sorry for not posting more often.
-
Nov 5th, 2009, 08:19 PM
#4
Re: String-Number Sorting
Leave the SortMode as Automatic and handle the SortCompare event. Check out the documentation for that event and you'll find a code example.
-
Nov 5th, 2009, 08:22 PM
#5
Thread Starter
Hyperactive Member
Re: String-Number Sorting
Thanks! You're a lifesaver, jmcilhinney!
Resolved.
"Why do all my attempts at science end with me getting punched by batman?" xkcd.
| Pong| |
Sorry for not posting more often.
-
Nov 6th, 2009, 12:26 AM
#6
Thread Starter
Hyperactive Member
Re: [RESOLVED] String-Number Sorting
Whoops, apparently not. I forgot to mention that I'm using a Datagridview bound to an access 2003 database. You may kick me now.
After building a sortCompare function which didn't work, I found the following statement hidden elsewhere in the documentation:
"The SortCompare event does not occur when the DataSource property is set or when the VirtualMode property value is true."
It would seem that this applies to all forms of custom sorting, including the Sort(IComparer) method as well.
Sorry about that.
Do you know how custom sorting can be implemented on a databound DataGridView?
(Edit) How annoying. Apparently you can't "Unresolve" a thread...
Last edited by Quasar6; Nov 6th, 2009 at 12:31 AM.
"Why do all my attempts at science end with me getting punched by batman?" xkcd.
| Pong| |
Sorry for not posting more often.
-
Nov 6th, 2009, 12:51 AM
#7
Re: [RESOLVED] String-Number Sorting
Sorry, I read that doco but apparently I read it incorrectly. I'll see what I can come up with. I'm sure I've done it before but I don't recall exactly how.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|