Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ R.slice(1, None, ['a', 'b', 'c', 'd']) # ['b', 'c', 'd']
```

- [x] sort
- [ ] sortBy
- [x] sortBy
- [ ] sortWith
- [ ] split
- [ ] splitAt
Expand Down
1 change: 1 addition & 0 deletions ramda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions ramda/sortBy.py
Original file line number Diff line number Diff line change
@@ -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)
49 changes: 49 additions & 0 deletions test/test_sortBy.py
Original file line number Diff line number Diff line change
@@ -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()