[lang-ref] ( sort_with_natural_order ) ( python )
def test_sort_with_natural_order():
pytest.skip('Not supported in Python')
def test_sort_with_natural_order_alternative():
# implement it
# Note: Use natsort library if you can
import re
def natural_sorted(items):
convert = lambda text: int(text) if text.isdigit() else text.lower()
sort_func = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(items, key = sort_func)
items = [
'12. L',
'2. B',
'11. K',
'3. C',
'22. V',
'21. U',
'1. A',
]
items_dict_order = [
'1. A',
'11. K',
'12. L',
'2. B',
'21. U',
'22. V',
'3. C',
]
items_natural_order = [
'1. A',
'2. B',
'3. C',
'11. K',
'12. L',
'21. U',
'22. V',
]
assert sorted(items) == items_dict_order
assert natural_sorted(items) == items_natural_order