diff --git a/src/utils.py b/src/utils.py index fe4f204b8..aaa3e06d8 100755 --- a/src/utils.py +++ b/src/utils.py @@ -377,6 +377,13 @@ def sortBy(f, L, cmp=cmp): for (i, elt) in enumerate(L): L[i] = L[i][2] +def sorted(iterable, *args, **kwargs): + """Returns a sorted list made from iterable. All other args are given to + list.sort unchanged.""" + L = list(iterable) + L.sort(*args, **kwargs) + return L + def mktemp(suffix=''): """Gives a decent random string, suitable for a filename.""" import sha diff --git a/test/test_utils.py b/test/test_utils.py index a4b46fe93..90bf42294 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -34,7 +34,6 @@ import sets import utils - class UtilsTest(unittest.TestCase): def testPluralize(self): f = utils.pluralize @@ -212,6 +211,14 @@ class UtilsTest(unittest.TestCase): L = ['supybot', 'Supybot'] utils.sortBy(str.lower, L) self.assertEqual(L, ['supybot', 'Supybot']) + + def testSorted(self): + L = ['a', 'c', 'b'] + self.assertEqual(utils.sorted(L), ['a', 'b', 'c']) + self.assertEqual(L, ['a', 'c', 'b']) + def mycmp(x, y): + return -cmp(x, y) + self.assertEqual(utils.sorted(L, mycmp), ['c', 'b', 'a']) def testNItems(self): self.assertEqual(utils.nItems(1, 'tool', 'crazy'), '1 crazy tool')