CodeProject - Sorting Strings for Humans with IComparer
![]()
"Introduction
A custom
IComparer
object can be used to provide the .Net sort algorithms an alternate strategy for ordering strings. TheIComparer
presented here attempts to interpret a string the way a human might, rather than the computer's strict lexicographical ordering.Background
A recent article at CodingHorror charged that "The default sort functions in almost every programming language are poorly suited for human consumption." The computer relies on a strict comparison, character by character, of the strings being sorted, but this is not the way that humans think of the strings.
...
Our goal, therefore, is to reform the computer's antisocial ways, allowing it to make its user a bit more comfortable.
Using the code
This goal is greatly simplified by the .Net framework. Microsoft has made some overtures itself toward the humanization of software built on the .Net platform: notice the built-in support for globalization. More specifically helping our purpose is the design principle allowing many default behaviors to be customized to the developer's own purposes, such as the many areas in which the Provider pattern has been implemented.
In our case, we can employ a custom-built
IComparer
to provide an alternate means of ordering the strings in a collection. Given theIComparer
presented here, using it rather than the built-in comparison of strings is trivial...."
A cool implementation of Human Sorting that was talked about quite a bit last week...
No comments:
Post a Comment
NOTE: Anonymous Commenting has been turned off for a while... The comment spammers are just killing me...
ALL comments are moderated. I will review every comment before it will appear on the blog.
Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...
I reserve, and will use, the right to not approve ANY comment for ANY reason. I will not usually, but if it's off topic, spam (or even close to spam-like), inflammatory, mean, etc, etc, well... then...
Please see my comment policy for more information if you are interested.
Thanks,
Greg
PS. I am proactively moderating comments. Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...