sort data like Windows Explorer sorts files??

have a Windows datagrid whose column values are
(1,10,100,1000,101,10-1,103,11,1710,2).  When the column's sorted (the
column's of type String), it'll sort this way
[1,10,100,1000,101,10-1,103,11,1710,2], which is wrong.  But in Windows
Explorer, when there are files with these same names , Explorer will sort
them like this: [1,2,10,10-1,11,100,101,103,1000,1710], which is correct.

How can I emulate this Windows explorer sort in my column?

Thanks.
LVL 2
MyersAAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AvonWyssConnect With a Mentor Commented:
Pad the strings with leading 0s.

string sortKey=columnValue.PadLeft(20, '0');
And then compare the sortkeys of the items you want to sort.
0
 
MyersAAuthor Commented:
Thanks.
Why does it sort correctly when I add the leading 0's?
0
 
AvonWyssCommented:
because...
  1 will be 00001 (if padded to 5 digits)
  2 will be 00002
10 will be 00010
etc.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
MyersAAuthor Commented:
And what about something like "1-100" or "10A"? How will it treat those?
0
 
hunor_namConnect With a Mentor Commented:
You could write youre own compare method to the grid...
Compare(object arg1, object arg2) from the IComparer interface.

It's easy to get something like this:
1,2,10,11,100,101,103,1000,1710,10-1,10-D

You would first check if the two items are numbers (isNum) if so, compare them normally, if bouth items are string, compare them as strings
IF one of the items is number and the other is string then the number is "smaller".
This way you could get a sort as described above... numbers in natural order, and then Strings in "natural" order.

If you don't understand something, just ask.

(Sorry i curently do not work in C#, this is the reason I don't give code, as i don't want to make syntax errors)
0
 
MyersAAuthor Commented:
I was assuming that Windows Explorer had a certain sorting mechanism that could be used. Isn't there an easier way to sort these without having to parse the string?
0
 
AvonWyssCommented:
My solution for instance? The very first one?
0
 
AvonWyssCommented:
TheLearnedOne, what is your recommendation?
0
 
hunor_namCommented:
Good question :)
0
All Courses

From novice to tech pro — start learning today.