Sort list by multiple keys and directions
Project description
mangosort: Sort list by multiple keys and directions
mangosort is a small module that helps you sort lists of dictionaries by multiple keys
Hot to use
Sort by multiple directions:
To sort by different directions at same time you need to pass a list of dictionaries with keys you want to order and values asc
or desc
.
from mangosort import mangosort
my_list = [{'code': 'beta', 'number': 3},
{'code': 'delta', 'number': 2},
{'code': 'delta', 'number': 3}]
mangosort.sort_by_key(my_list, [{'code': 'desc'}, {'number': 'asc'}])
[{'code': 'delta', 'number': 2},
{'code': 'delta', 'number': 3},
{'code': 'beta', 'number': 3}]
If you need or prefer to pass bool, in this case, think reverse True
(desc) or False
(asc).
# Reverse True or False
mangosort.sort_by_key(my_list, [{'code': True}, {'number': 0}])
[{'code': 'delta', 'number': 2},
{'code': 'delta', 'number': 3},
{'code': 'beta', 'number': 3}]
Sort ASC:
from mangosort import mangosort
my_list = [{'code': 'beta', 'number': 3},
{'code': 'delta', 'number': 2},
{'code': 'delta', 'number': 3}]
mangosort.sort_by_key_asc(my_list, ['code', 'number'])
[{'code': 'beta', 'number': 3},
{'code': 'delta', 'number': 2},
{'code': 'delta', 'number': 3}]
Sort DESC:
from mangosort import mangosort
my_list = [{'code': 'beta', 'number': 3},
{'code': 'delta', 'number': 2},
{'code': 'delta', 'number': 3}]
mangosort.sort_by_key_asc(my_list, ['code', 'number'])
[{'code': 'delta', 'number': 3},
{'code': 'delta', 'number': 2},
{'code': 'beta', 'number': 3}]
How to Install
Install via PyPi
pip install mangosort
Contributing
I’m accepting pull requests that improve speed and legibility of the code or anything else you think will be good.
License
This module is under MIT License, but you can do what you want. It's just a bunch of code.
Just tell your friends about it.
Communism will win!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mangosort-0.2.1.tar.gz
(2.4 kB
view hashes)
Built Distribution
Close
Hashes for mangosort-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c66e242136d89ee3b76bf4ef7e3c5008b32dbaa91df737fb77bf8ee6f590363 |
|
MD5 | faed9e3b70d7745865b98afee03e97ac |
|
BLAKE2b-256 | 96c143d8ad76418aba934deafb528b0126332c480fa7471e822e448069c8f2b4 |