PickwickSoft / pystreamapi

The Python Stream API Library offering Streams like you know from Java/Kotlin/Scala in Python with some cool extensions
https://pystreamapi.pickwicksoft.org/
GNU General Public License v3.0
29 stars 4 forks source link

Add comparator argument to `sorted()`method #15

Closed garlontas closed 2 years ago

garlontas commented 2 years ago

The sorted()method currently does not support comparators. It uses Python's default sorted function.

An overloaded method like that would be useful

def compare(a, b):
    return a[0] - b[0]

Stream.of([(8, None), (7, None), (5, None), (4, None)]) \
    .sorted(comparator) \
    .to_list() # [(4, None), (5, None), (7, None), (8, None)]

Internally, it could use functools.cmp_to_key() combined with the

sorted(data, key=cmp_to_key(compare))