OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 """Intelligent natural sort implementation.""" | |
6 | |
7 import re | |
8 | |
9 | |
10 def natcmp(a, b): | |
11 """Natural string comparison, case sensitive.""" | |
12 try_int = lambda s: int(s) if s.isdigit() else s | |
13 def natsort_key(s): | |
14 if not isinstance(s, basestring): | |
15 # Since re.findall() generates a list out of a string, returns a list here | |
16 # to balance the comparison done in cmp(). | |
17 return [s] | |
18 return map(try_int, re.findall(r'(\d+|\D+)', s)) | |
19 return cmp(natsort_key(a), natsort_key(b)) | |
20 | |
21 | |
22 def try_lower(x): | |
23 """Opportunistically lower() a string if it is a string.""" | |
24 return x.lower() if hasattr(x, 'lower') else x | |
25 | |
26 | |
27 def naticasecmp(a, b): | |
28 """Natural string comparison, ignores case.""" | |
29 return natcmp(try_lower(a), try_lower(b)) | |
30 | |
31 | |
32 def natsort(seq, cmp=natcmp, *args, **kwargs): # pylint: disable=W0622 | |
33 """In-place natural string sort. | |
34 >>> a = ['3A2', '3a1'] | |
35 >>> natsort(a, key=try_lower) | |
36 >>> a | |
37 ['3a1', '3A2'] | |
38 >>> a = ['3a2', '3A1'] | |
39 >>> natsort(a, key=try_lower) | |
40 >>> a | |
41 ['3A1', '3a2'] | |
42 >>> a = ['3A2', '3a1'] | |
43 >>> natsort(a, cmp=naticasecmp) | |
44 >>> a | |
45 ['3a1', '3A2'] | |
46 >>> a = ['3a2', '3A1'] | |
47 >>> natsort(a, cmp=naticasecmp) | |
48 >>> a | |
49 ['3A1', '3a2'] | |
50 """ | |
51 seq.sort(cmp=cmp, *args, **kwargs) | |
52 | |
53 | |
54 def natsorted(seq, cmp=natcmp, *args, **kwargs): # pylint: disable=W0622 | |
55 """Returns a copy of seq, sorted by natural string sort. | |
56 | |
57 >>> natsorted(i for i in [4, '3a', '2', 1]) | |
58 [1, '2', '3a', 4] | |
59 >>> natsorted(['a4', 'a30']) | |
60 ['a4', 'a30'] | |
61 >>> natsorted(['3A2', '3a1'], key=try_lower) | |
62 ['3a1', '3A2'] | |
63 >>> natsorted(['3a2', '3A1'], key=try_lower) | |
64 ['3A1', '3a2'] | |
65 >>> natsorted(['3A2', '3a1'], cmp=naticasecmp) | |
66 ['3a1', '3A2'] | |
67 >>> natsorted(['3a2', '3A1'], cmp=naticasecmp) | |
68 ['3A1', '3a2'] | |
69 >>> natsorted(['3A2', '3a1']) | |
70 ['3A2', '3a1'] | |
71 >>> natsorted(['3a2', '3A1']) | |
72 ['3A1', '3a2'] | |
73 """ | |
74 return sorted(seq, cmp=cmp, *args, **kwargs) | |
75 | |
76 | |
77 if __name__ == '__main__': | |
78 import doctest | |
79 doctest.testmod() | |
OLD | NEW |