From 039c56aada41c6116fd57bc7812fd2da93bb2203 Mon Sep 17 00:00:00 2001 From: zyd Date: Wed, 11 May 2022 22:36:17 +0900 Subject: [PATCH] add method sortBy --- README.md | 2 +- ramda/__init__.py | 1 + ramda/sortBy.py | 19 ++++++++++++++++++ test/test_sortBy.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 ramda/sortBy.py create mode 100644 test/test_sortBy.py diff --git a/README.md b/README.md index 5caf0b2..32dd7be 100644 --- a/README.md +++ b/README.md @@ -428,7 +428,7 @@ R.slice(1, None, ['a', 'b', 'c', 'd']) # ['b', 'c', 'd'] ``` - [x] sort -- [ ] sortBy +- [x] sortBy - [ ] sortWith - [ ] split - [ ] splitAt diff --git a/ramda/__init__.py b/ramda/__init__.py index 3c7c37b..7023271 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -72,6 +72,7 @@ from .reverse import reverse from .slice import slice from .sort import sort +from .sortBy import sortBy from .sum import sum from .T import T from .tail import tail diff --git a/ramda/sortBy.py b/ramda/sortBy.py new file mode 100644 index 0000000..398291f --- /dev/null +++ b/ramda/sortBy.py @@ -0,0 +1,19 @@ +from functools import cmp_to_key + +from .private._curry2 import _curry2 + + +def inner_sortBy(fn, arr): + def comparator(a, b): + aa = fn(a) + bb = fn(b) + if aa < bb: + return -1 + elif aa > bb: + return 1 + else: + return 0 + return sorted(arr, key=cmp_to_key(comparator)) + + +sortBy = _curry2(inner_sortBy) diff --git a/test/test_sortBy.py b/test/test_sortBy.py new file mode 100644 index 0000000..8ed65ab --- /dev/null +++ b/test/test_sortBy.py @@ -0,0 +1,49 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/sortBy.js +""" + +albums = [ + {'title': 'Art of the Fugue', 'artist': 'Glenn Gould', 'genre': 'Baroque'}, + {'title': 'A Farewell to Kings', 'artist': 'Rush', 'genre': 'Rock'}, + {'title': 'Timeout', 'artist': 'Dave Brubeck Quartet', 'genre': 'Jazz'}, + {'title': 'Fly By Night', 'artist': 'Rush', 'genre': 'Rock'}, + {'title': 'Goldberg Variations', 'artist': 'Daniel Barenboim', 'genre': 'Baroque'}, + {'title': 'New World Symphony', 'artist': 'Leonard Bernstein', 'genre': 'Romantic'}, + {'title': 'Romance with the Unseen', 'artist': 'Don Byron', 'genre': 'Jazz'}, + {'title': 'Somewhere In Time', 'artist': 'Iron Maiden', 'genre': 'Metal'}, + {'title': 'In Times of Desparation', 'artist': 'Danny Holt', 'genre': 'Modern'}, + {'title': 'Evita', 'artist': 'Various', 'genre': 'Broadway'}, + {'title': 'Five Leaves Left', 'artist': 'Nick Drake', 'genre': 'Folk'}, + {'title': 'The Magic Flute', 'artist': 'John Eliot Gardiner', 'genre': 'Classical'} +] + + +class TestSortBy(unittest.TestCase): + def test_sorts_by_a_simple_property_of_the_objects(self): + sortedAlbums = R.sortBy(R.prop('title'), albums) + self.assertEqual(len(albums), len(sortedAlbums)) + self.assertEqual('A Farewell to Kings', sortedAlbums[0]['title']) + self.assertEqual('Timeout', sortedAlbums[11]['title']) + + def test_preserves_object_identity(self): + a = {'value': 'a'} + b = {'value': 'b'} + result = R.sortBy(R.prop('value'), [a, b]) + self.assertEqual(a, result[0]) + self.assertEqual(b, result[1]) + + def test_sorts_array_like_object(self): + args = (lambda *args: args)('c', 'a', 'b') + result = R.sortBy(R.identity, args) + self.assertEqual('a', result[0]) + self.assertEqual('b', result[1]) + self.assertEqual('c', result[2]) + + +if __name__ == '__main__': + unittest.main()