Mercurial Hosting > traffic-intelligence
diff python/utils.py @ 733:c35e4a4b199d dev
sortbylength
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 11 Aug 2015 12:06:10 -0400 |
parents | 8d99a9e16644 |
children | fb60b54e1041 |
line wrap: on
line diff
--- a/python/utils.py Tue Aug 11 11:49:01 2015 -0400 +++ b/python/utils.py Tue Aug 11 12:06:10 2015 -0400 @@ -233,6 +233,19 @@ xsorted = sorted(D.keys()) return xsorted, [D[x] for x in xsorted] +def compareLengthForSort(i, j): + if len(i) < len(j): + return -1 + elif len(i) == len(j): + return 0 + else: + return 1 + +def sortByLength(instances, reverse = False): + '''Returns a new list with the instances sorted by length (method __len__) + reverse is passed to sorted''' + return sorted(instances, cmp = compareLengthForSort, reverse = reverse) + def ceilDecimals(v, nDecimals): '''Rounds the number at the nth decimal eg 1.23 at 0 decimal is 2, at 1 decimal is 1.3'''