New GitHub issue #118841 from brass75:<br>
<hr>
<pre>
# Feature or enhancement
### Proposal:
Very often we have to sort text and need the ability to sort numerically as opposed to semantically for text that includes numeric data. For example if we want to sort this list of numbers and list of numeric character strings we should have the ability in `list.sort` or `sorted` to specify `numeric_sort` for iterables we want to sort that we know contain numeric data.
```python
>>> sorted(['135', '136', '1359', '13542'])
['135', '13542', '1359', '136']
>>> sorted([135, 136, 1359, 13542])
[135, 136, 1359, 13542]
```
In the case above we end up with different sort orders based on whether we are sorting as `str` or as `int`. There are many cases, such as if we want to sort a list of versions, where being able to sort strings while still treating numbers as numbers is important.
```python
>>> sorted(['32.135.0', '311.1359.578', '31.1359.5779', '23.136.1340'])
['23.136.1340', '31.1359.5779', '311.1359.578', '32.135.0']
```
The sort order of the semver strings in the section above is incorrect. It should be:
```python
[ '23.136.1340', '31.1359.5779', '32.135.0', '311.1359.578']
```
The feature proposal is to add a `numeric_sort_key` that can be used with calls to `list.sort`, `sorted`, and other functions and methods that take a sorting key (itertools.groupby, etc.)
### Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
### Links to previous discussion of this feature:
_No response_
</pre>
<hr>
<a href="https://github.com/python/cpython/issues/118841">View on GitHub</a>
<p>Labels: type-feature</p>
<p>Assignee: </p>