Solved

sort data like Windows Explorer sorts files??

Posted on 2004-09-14
12
328 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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