MCPcopy Create free account
hub / github.com/dask/dask / natural_sort_key

Function natural_sort_key

dask/utils.py:1556–1582  ·  view source on GitHub ↗

Sorting `key` function for performing a natural sort on a collection of strings See https://en.wikipedia.org/wiki/Natural_sort_order Parameters ---------- s : str A string that is an element of the collection being sorted Returns ------- tuple[str or i

(s: str)

Source from the content-addressed store, hash-verified

1554
1555
1556def natural_sort_key(s: str) -> list[str | int]:
1557 """
1558 Sorting `key` function for performing a natural sort on a collection of
1559 strings
1560
1561 See https://en.wikipedia.org/wiki/Natural_sort_order
1562
1563 Parameters
1564 ----------
1565 s : str
1566 A string that is an element of the collection being sorted
1567
1568 Returns
1569 -------
1570 tuple[str or int]
1571 Tuple of the parts of the input string where each part is either a
1572 string or an integer
1573
1574 Examples
1575 --------
1576 >>> a = ['f0', 'f1', 'f2', 'f8', 'f9', 'f10', 'f11', 'f19', 'f20', 'f21']
1577 >>> sorted(a)
1578 ['f0', 'f1', 'f10', 'f11', 'f19', 'f2', 'f20', 'f21', 'f8', 'f9']
1579 >>> sorted(a, key=natural_sort_key)
1580 ['f0', 'f1', 'f2', 'f8', 'f9', 'f10', 'f11', 'f19', 'f20', 'f21']
1581 """
1582 return [int(part) if part.isdigit() else part for part in re.split(r"(\d+)", s)]
1583
1584
1585def parse_bytes(s: float | str) -> int:

Callers 1

Calls 1

splitMethod · 0.80

Tested by

no test coverage detected