Edgewall Software

Ticket #1038: natural_order_sort_for_milestones.diff

File natural_order_sort_for_milestones.diff, 3.3 KB (added by cboos, 3 years ago)

Use natural ordering for Milestones in the roadmap view

  • trac/ticket/roadmap.py

     
    2121from trac.core import * 
    2222from trac.perm import IPermissionRequestor 
    2323from 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 
    2526from trac.ticket import Milestone, Ticket, TicketSystem 
    2627from trac.Timeline import ITimelineEventProvider 
    2728from trac.web import IRequestHandler 
     
    154155        for idx, milestone in enumerate(Milestone.select(self.env, showall)): 
    155156            hdf = milestone_to_hdf(self.env, db, req, milestone) 
    156157            milestones.append(hdf) 
     158        milestones.sort(lambda a, b: natural_order(a['name'], b['name'])) 
    157159        req.hdf['roadmap.milestones'] = milestones 
    158160 
    159161        for idx,milestone in enumerate(milestones): 
  • trac/versioncontrol/web_ui/browser.py

     
    3131 
    3232CHUNK_SIZE = 4096 
    3333 
    34 DIGITS = re.compile(r'[0-9]+') 
    35 def _natural_order(x, y): 
    36     """Comparison function for natural order sorting based on 
    37     http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/214202.""" 
    38     nx = ny = 0 
    39     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()]) or 
    45              cmp(int(x[a.start():a.end()]), int(y[b.start():b.end()]))) 
    46         if r: 
    47             return r 
    48         nx, ny = a.end(), b.end() 
    4934 
    50  
    5135class BrowserModule(Component): 
    5236 
    5337    implements(INavigationContributor, IPermissionRequestor, IRequestHandler, 
     
    157141            elif order == 'size': 
    158142                return neg * cmp(a['content_length'], b['content_length']) 
    159143            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()) 
    162146        info.sort(cmp_func) 
    163147 
    164148        req.hdf['browser.items'] = info 
  • trac/util.py

     
    260260            shortline = text[:i]+' ...' 
    261261    return shortline 
    262262 
     263DIGITS = re.compile(r'[0-9]+') 
     264def 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 
    263279def hex_entropy(bytes=32): 
    264280    import md5 
    265281    import random