Solved

sort data like Windows Explorer sorts files??

Posted on 2004-09-14
12
329 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 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