RSS

How To: Natural Sorting / Human Sorting in Python and other languages

21 Dec

There are some subtle and crucial concepts that I meet again and again. The problem is that once I’m done with them sometimes I tend to forget some of them. Sorting string variables in a humane (alphabetical, natural) instead of ASCIIbetical order is just one of those concepts. I had to re-discover this issue as I was trying to write a file renaming program in Python today. My problem can be described as


>>> "u11-Phrase 099.wav" >> "u11-Phrase 100.wav" >> "u11-Phrase 101.wav" < "u11-Phrase 1000.wav"
False

So according Python (or your favorite language’s default sort functionality!) “u11-Phrase 100.wav” comes before “u11-Phrase 1000.wav” but “u11-Phrase 101.wav” comes after “u11-Phrase 1000.wav”!

For solutions please see one of those:

Advertisements
 
2 Comments

Posted by on December 21, 2009 in General, Programlama

 

Tags:

2 responses to “How To: Natural Sorting / Human Sorting in Python and other languages

  1. bsod1

    December 22, 2009 at 19:29

    Python Cookbook(O’Reilly)’de bundan bahsediliyordu yanlış hatırlamıyorsam, güzel bir kaynak..

     
  2. Atamert Ölçgen

    December 25, 2009 at 11:12

    It’s nice to see you using StackOverflow.

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: