Tuesday, December 18, 2007

Human Sorting via IComparer

CodeProject - Sorting Strings for Humans with IComparer

untitled

"Introduction

A custom IComparer object can be used to provide the .Net sort algorithms an alternate strategy for ordering strings. The IComparer 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 the IComparer 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: