Solved

sort data like Windows Explorer sorts files??

Posted on 2004-09-14
12
326 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
 
LVL 2

Author Comment

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

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now