[Tutor] sorting file paths made up of of strings and numbers

Emad Nawfal (عماد نوفل) emadnawfal at gmail.com
Wed Apr 15 01:39:21 CEST 2009


Hi tutors,
How can I sort the following list in a way that takes care of the right
order of numbers? The sorted function compares strings here as far as I can
see, but I want to have filepath2 follow filepath1. Your help is
appreciated.
>>> myList
['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89',
'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15',
'filepath88', 'filepath70', 'filepath33', 'filepath58', 'filepath28',
'filepath61', 'filepath23', 'filepath64', 'filepath72', 'filepath11',
'filepath63', 'filepath76', 'filepath87', 'filepath45', 'filepath41',
'filepath68', 'filepath81', 'filepath75', 'filepath62', 'filepath46',
'filepath36', 'filepath39', 'filepath5', 'filepath34', 'filepath8',
'filepath67', 'filepath14', 'filepath82', 'filepath29', 'filepath47',
'filepath79', 'filepath13', 'filepath16', 'filepath71', 'filepath27',
'filepath100', 'filepath20', 'filepath92', 'filepath57', 'filepath73',
'filepath40', 'filepath44', 'filepath83', 'filepath50', 'filepath66',
'filepath94', 'filepath86', 'filepath22', 'filepath17', 'filepath84',
'filepath38', 'filepath12', 'filepath53', 'filepath6', 'filepath48',
'filepath60', 'filepath51', 'filepath90', 'filepath4', 'filepath78',
'filepath69', 'filepath35', 'filepath97', 'filepath93', 'filepath24',
'filepath26', 'filepath1', 'filepath3', 'filepath96', 'filepath77',
'filepath98', 'filepath18', 'filepath43', 'filepath19', 'filepath56',
'filepath65', 'filepath95', 'filepath55', 'filepath7', 'filepath99',
'filepath91', 'filepath85', 'filepath9', 'filepath59', 'filepath10',
'filepath30', 'filepath31', 'filepath80', 'filepath42', 'filepath74',
'filepath21']
>>> sorted(myList)
['filepath0', 'filepath1', 'filepath10', 'filepath100', 'filepath11',
'filepath12', 'filepath13', 'filepath14', 'filepath15', 'filepath16',
'filepath17', 'filepath18', 'filepath19', 'filepath2', 'filepath20',
'filepath21', 'filepath22', 'filepath23', 'filepath24', 'filepath25',
'filepath26', 'filepath27', 'filepath28', 'filepath29', 'filepath3',
'filepath30', 'filepath31', 'filepath32', 'filepath33', 'filepath34',
'filepath35', 'filepath36', 'filepath37', 'filepath38', 'filepath39',
'filepath4', 'filepath40', 'filepath41', 'filepath42', 'filepath43',
'filepath44', 'filepath45', 'filepath46', 'filepath47', 'filepath48',
'filepath49', 'filepath5', 'filepath50', 'filepath51', 'filepath52',
'filepath53', 'filepath54', 'filepath55', 'filepath56', 'filepath57',
'filepath58', 'filepath59', 'filepath6', 'filepath60', 'filepath61',
'filepath62', 'filepath63', 'filepath64', 'filepath65', 'filepath66',
'filepath67', 'filepath68', 'filepath69', 'filepath7', 'filepath70',
'filepath71', 'filepath72', 'filepath73', 'filepath74', 'filepath75',
'filepath76', 'filepath77', 'filepath78', 'filepath79', 'filepath8',
'filepath80', 'filepath81', 'filepath82', 'filepath83', 'filepath84',
'filepath85', 'filepath86', 'filepath87', 'filepath88', 'filepath89',
'filepath9', 'filepath90', 'filepath91', 'filepath92', 'filepath93',
'filepath94', 'filepath95', 'filepath96', 'filepath97', 'filepath98',
'filepath99']
>>> ```

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
--------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090414/b736897c/attachment.htm>


More information about the Tutor mailing list