Solved

sort data like Windows Explorer sorts files??

Posted on 2004-09-14
12
331 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 38 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 37 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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