From d8886d4c9ff4d2af7f7c582339b8443df349273f 2008-07-04 16:29:48 From: Ville M. Vainio Date: 2008-07-04 16:29:48 Subject: [PATCH] string list 'sort(field, nums = True)' method --- diff --git a/IPython/genutils.py b/IPython/genutils.py index 4bc418d..90747b0 100644 --- a/IPython/genutils.py +++ b/IPython/genutils.py @@ -1137,10 +1137,31 @@ class SList(list): res.append(" ".join(lineparts)) return res - - - + def sort(self,field, nums = False): + """ sort by specified fields (see fields()) + + Example:: + a.sort(1, nums = True) + + Sorts a by second field, in numerical order (so that 21 > 3) + """ + + #decorate, sort, undecorate + dsu = [[SList([line]).fields(field), line] for line in self] + if nums: + for i in range(len(dsu)): + numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()]) + print numstr + try: + n = int(numstr) + except ValueError: + n = 0; + dsu[i][0] = n + + + dsu.sort() + return [t[1] for t in dsu] def print_slist(arg): """ Prettier (non-repr-like) and more informative printer for SList """ diff --git a/docs/source/changes.txt b/docs/source/changes.txt index 89fe83b..529f315 100644 --- a/docs/source/changes.txt +++ b/docs/source/changes.txt @@ -29,7 +29,11 @@ New features Development Team" as the copyright holder. We give more details about exactly what this means in this file. All developer should read this and use the new banner in all IPython source code files. - * sh profile: ./foo runs foo as system command, no need to do !./foo anymore + * sh profile: ./foo runs foo as system command, no need to do !./foo anymore + * String lists now support 'sort(field, nums = True)' method (to easily + sort system command output). Try it with 'a = !ls -l ; a.sort(1, nums=1)' + + Bug fixes ---------