core.sort¶
Functions¶
arg_sort_int_str ¶
Return indices for natural sorting of strings with embedded numbers.
Returns the indices of strings in the order they would appear if sorted using natural ordering (numeric parts sorted numerically).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str_int | list[str] | List of strings to get sort indices for. | required |
reverse | bool | If True, returns indices for descending order. | False |
Returns:
| Type | Description |
|---|---|
list[int] | List[int]: List of indices in natural sort order. |
Examples:
>>> arg_sort_int_str(["file1.txt", "file10.txt", "file2.txt"])
[0, 2, 1]
>>> arg_sort_int_str(["abc9", "abc12", "abc100"])
[0, 1, 2]
Source code in pyadvtools/core/sort.py
arg_sorted ¶
Return indices that would sort the list.
Returns the indices of elements in the order they would appear if the list were sorted, without actually sorting the original list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elements | list | List of elements to get sort indices for. | required |
reverse | bool | If True, returns indices for descending order. | False |
Returns:
| Type | Description |
|---|---|
list[int] | List[int]: List of indices in sort order. |
Examples:
>>> arg_sorted([3, 1, 4, 1, 5])
[1, 3, 0, 2, 4]
>>> arg_sorted([3, 1, 4, 1, 5], reverse=True)
[4, 2, 0, 1, 3]
Source code in pyadvtools/core/sort.py
sort_int_str ¶
Sort strings with natural numeric ordering.
Sorts a list of strings using natural ordering where embedded numbers are sorted numerically rather than lexicographically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str_int | list[str] | List of strings to sort. | required |
reverse | bool | If True, sorts in descending order. | False |
Returns:
| Type | Description |
|---|---|
list[str] | List[str]: New list with strings sorted using natural ordering. |
Examples:
>>> sort_int_str(["file1.txt", "file10.txt", "file2.txt"])
['file1.txt', 'file2.txt', 'file10.txt']
>>> sort_int_str(["abc9", "abc12", "abc100"])
['abc9', 'abc12', 'abc100']
Source code in pyadvtools/core/sort.py
sort_strings_with_embedded_numbers ¶
Split string into parts for natural sorting with embedded numbers.
Splits a string into alternating text and numeric parts, converting numeric parts to integers for proper natural sorting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s | str | String to split into sortable parts. | required |
Returns:
| Type | Description |
|---|---|
list[str] | List[str]: List of alternating text and integer parts. |
Examples:
>>> sort_strings_with_embedded_numbers("abc123def456")
['abc', 123, 'def', 456]
>>> sort_strings_with_embedded_numbers("file10.txt")
['file', 10, '.txt']