##// END OF EJS Templates
string list 'sort(field, nums = True)' method
Ville M. Vainio -
Show More
@@ -1137,10 +1137,31 b' class SList(list):'
1137 res.append(" ".join(lineparts))
1137 res.append(" ".join(lineparts))
1138
1138
1139 return res
1139 return res
1140
1140 def sort(self,field, nums = False):
1141
1141 """ sort by specified fields (see fields())
1142
1142
1143 Example::
1144 a.sort(1, nums = True)
1145
1146 Sorts a by second field, in numerical order (so that 21 > 3)
1143
1147
1148 """
1149
1150 #decorate, sort, undecorate
1151 dsu = [[SList([line]).fields(field), line] for line in self]
1152 if nums:
1153 for i in range(len(dsu)):
1154 numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()])
1155 print numstr
1156 try:
1157 n = int(numstr)
1158 except ValueError:
1159 n = 0;
1160 dsu[i][0] = n
1161
1162
1163 dsu.sort()
1164 return [t[1] for t in dsu]
1144
1165
1145 def print_slist(arg):
1166 def print_slist(arg):
1146 """ Prettier (non-repr-like) and more informative printer for SList """
1167 """ Prettier (non-repr-like) and more informative printer for SList """
@@ -29,7 +29,11 b' New features'
29 Development Team" as the copyright holder. We give more details about exactly
29 Development Team" as the copyright holder. We give more details about exactly
30 what this means in this file. All developer should read this and use the new
30 what this means in this file. All developer should read this and use the new
31 banner in all IPython source code files.
31 banner in all IPython source code files.
32 * sh profile: ./foo runs foo as system command, no need to do !./foo anymore
32 * sh profile: ./foo runs foo as system command, no need to do !./foo anymore
33 * String lists now support 'sort(field, nums = True)' method (to easily
34 sort system command output). Try it with 'a = !ls -l ; a.sort(1, nums=1)'
35
36
33
37
34 Bug fixes
38 Bug fixes
35 ---------
39 ---------
General Comments 0
You need to be logged in to leave comments. Login now