Skip to content

Instantly share code, notes, and snippets.

@ryonsherman
Last active December 20, 2015 19:49
Show Gist options
  • Save ryonsherman/ab557853e3a7de24a024 to your computer and use it in GitHub Desktop.
Save ryonsherman/ab557853e3a7de24a024 to your computer and use it in GitHub Desktop.
Sort numerical values regardless of format
#!/usr/bin/env python2
numsort = lambda l: sorted(l, key=lambda x: int(str(x).split('-')[0].split('.')[0].replace('$', '')))
@ryonsherman
Copy link
Author

Example

>>> list1 = ['$1.00-$2.00', '$22.00-$23.00', '$2.00-$3.00', '$3.00-$4.00']
>>> list2 = [1, 22, 2, 3]
>>> list3 = [1, 22, '2', '3']

>>> print "\n".join(map(str, map(numsort, (list1, list2, list3))))
['$1.00-$2.00', '$2.00-$3.00', '$3.00-$4.00', '$22.00-$23.00']
[1, 2, 3, 22]
[1, '2', '3', 22]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment