Skip to content

Commit 9cdfe10

Browse files
committed
build
1 parent 457c999 commit 9cdfe10

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"map_ex": "01_foundation.ipynb",
4545
"filter_ex": "01_foundation.ipynb",
4646
"range_of": "02_utils.ipynb",
47+
"sorted_ex": "01_foundation.ipynb",
4748
"listable_types": "01_foundation.ipynb",
4849
"renumerate": "01_foundation.ipynb",
4950
"first": "01_foundation.ipynb",

fastcore/foundation.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
__all__ = ['defaults', 'copy_func', 'patch_to', 'patch', 'patch_property', 'add_docs', 'docs', 'custom_dir', 'arg0',
44
'arg1', 'arg2', 'arg3', 'arg4', 'coll_repr', 'is_bool', 'mask2idxs', 'cycle', 'zip_cycle', 'is_indexer',
55
'negate_func', 'GetAttr', 'delegate_attr', 'bind', 'argwhere', 'map_ex', 'filter_ex', 'range_of',
6-
'listable_types', 'renumerate', 'first', 'nested_attr', 'stop', 'tst', 'tst2', 'CollBase', 'L',
6+
'sorted_ex', 'listable_types', 'renumerate', 'first', 'nested_attr', 'stop', 'tst', 'tst2', 'CollBase', 'L',
77
'save_config_file', 'read_config_file', 'Config']
88

99
# Cell
@@ -216,6 +216,14 @@ def range_of(a, b=None, step=None):
216216
if is_coll(a): a = len(a)
217217
return list(range(a,b,step) if step is not None else range(a,b) if b is not None else range(a))
218218

219+
# Cell
220+
def sorted_ex(iterable, key=None, reverse=False):
221+
"Like `sorted`, but if key is str use `attrgetter`; if int use `itemgetter`"
222+
if isinstance(key,str): k=lambda o:getattr(o,key,0)
223+
elif isinstance(key,int): k=itemgetter(key)
224+
else: k=key
225+
return sorted(iterable, key=k, reverse=reverse)
226+
219227
# Cell
220228
listable_types = typing.Collection,Generator,map,filter,zip
221229

0 commit comments

Comments
 (0)