Package elisa :: Package core :: Package utils :: Module sorting
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.utils.sorting

 1  # -*- coding: utf-8 -*- 
 2  # Elisa - Home multimedia server 
 3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
 4  # All rights reserved. 
 5  # 
 6  # This file is available under one of two license agreements. 
 7  # 
 8  # This file is licensed under the GPL version 3. 
 9  # See "LICENSE.GPL" in the root of this distribution including a special 
10  # exception to use Elisa with Fluendo's plugins. 
11  # 
12  # The GPL part of Elisa is also available under a commercial licensing 
13  # agreement from Fluendo. 
14  # See "LICENSE.Elisa" in the root directory of this distribution package 
15  # for details on that license. 
16   
17  """ 
18  Utility functions for sorting 
19  """ 
20   
21  __maintainer__ = 'Florian Boucault <florian@fluendo.com>' 
22   
23   
24  import re 
25   
26  natural_re = re.compile('(\d+|\D+)') 
27   
28 -def _try_int(s):
29 try: 30 return int(s) 31 except ValueError: 32 return s
33
34 -def _natural_key(s):
35 return [_try_int(i) for i in natural_re.findall(s.lower())]
36
37 -def natural_sort(l):
38 """ 39 Case insensitive human compliant sort. 40 41 @param l: list that needs to be sorted 42 @type l: list of string 43 """ 44 l.sort(key=_natural_key)
45