Ticket #1038: natural_order_sort_for_milestones.diff
| File natural_order_sort_for_milestones.diff, 3.3 KB (added by cboos, 3 years ago) |
|---|
-
trac/ticket/roadmap.py
21 21 from trac.core import * 22 22 from trac.perm import IPermissionRequestor 23 23 from trac.util import escape, format_date, format_datetime, parse_date, \ 24 pretty_timedelta, shorten_line, unescape, CRLF, Markup 24 pretty_timedelta, shorten_line, unescape, CRLF, Markup, \ 25 natural_order 25 26 from trac.ticket import Milestone, Ticket, TicketSystem 26 27 from trac.Timeline import ITimelineEventProvider 27 28 from trac.web import IRequestHandler … … 154 155 for idx, milestone in enumerate(Milestone.select(self.env, showall)): 155 156 hdf = milestone_to_hdf(self.env, db, req, milestone) 156 157 milestones.append(hdf) 158 milestones.sort(lambda a, b: natural_order(a['name'], b['name'])) 157 159 req.hdf['roadmap.milestones'] = milestones 158 160 159 161 for idx,milestone in enumerate(milestones): -
trac/versioncontrol/web_ui/browser.py
31 31 32 32 CHUNK_SIZE = 4096 33 33 34 DIGITS = re.compile(r'[0-9]+')35 def _natural_order(x, y):36 """Comparison function for natural order sorting based on37 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/214202."""38 nx = ny = 039 while True:40 a = DIGITS.search(x, nx)41 b = DIGITS.search(y, ny)42 if None in (a, b):43 return cmp(x[nx:], y[ny:])44 r = (cmp(x[nx:a.start()], y[ny:b.start()]) or45 cmp(int(x[a.start():a.end()]), int(y[b.start():b.end()])))46 if r:47 return r48 nx, ny = a.end(), b.end()49 34 50 51 35 class BrowserModule(Component): 52 36 53 37 implements(INavigationContributor, IPermissionRequestor, IRequestHandler, … … 157 141 elif order == 'size': 158 142 return neg * cmp(a['content_length'], b['content_length']) 159 143 else: 160 return neg * _natural_order(a['name'].lower(),161 b['name'].lower())144 return neg * util.natural_order(a['name'].lower(), 145 b['name'].lower()) 162 146 info.sort(cmp_func) 163 147 164 148 req.hdf['browser.items'] = info -
trac/util.py
260 260 shortline = text[:i]+' ...' 261 261 return shortline 262 262 263 DIGITS = re.compile(r'[0-9]+') 264 def natural_order(x, y): 265 """Comparison function for natural order sorting based on 266 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/214202.""" 267 nx = ny = 0 268 while True: 269 a = DIGITS.search(x, nx) 270 b = DIGITS.search(y, ny) 271 if None in (a, b): 272 return cmp(x[nx:], y[ny:]) 273 r = (cmp(x[nx:a.start()], y[ny:b.start()]) or 274 cmp(int(x[a.start():a.end()]), int(y[b.start():b.end()]))) 275 if r: 276 return r 277 nx, ny = a.end(), b.end() 278 263 279 def hex_entropy(bytes=32): 264 280 import md5 265 281 import random
