Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sort data like Windows Explorer sorts files??

Posted on 2004-09-14
12
Medium Priority
?
333 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:MyersA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
12 Comments
 
LVL 14

Accepted Solution

by:
AvonWyss earned 152 total points
ID: 12059180
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
 
LVL 2

Author Comment

by:MyersA
ID: 12059487
Thanks.
Why does it sort correctly when I add the leading 0's?
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12059505
because...
  1 will be 00001 (if padded to 5 digits)
  2 will be 00002
10 will be 00010
etc.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:MyersA
ID: 12060171
And what about something like "1-100" or "10A"? How will it treat those?
0
 

Assisted Solution

by:hunor_nam
hunor_nam earned 148 total points
ID: 12062675
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
 
LVL 2

Author Comment

by:MyersA
ID: 12183167
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12183673
My solution for instance? The very first one?
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12393799
TheLearnedOne, what is your recommendation?
0
 

Expert Comment

by:hunor_nam
ID: 12398408
Good question :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question