Python Forum
sorting with numbers in text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting with numbers in text
#3
(Jul-17-2021, 11:48 PM)Skaperen Wrote: before i start coding this, does anyone know of an existing function to do this?
Yes the problem is often called Human or Natural Sorting,
write a own function like DeaD_EyE is a common way,there also module like natsort
natsor works for many common cases.
>>> from natsort import natsorted
>>> 
>>> versions = ['version-1.9', 'version-2.0', 'version-1.11', 'version-1.10']
>>> sorted(versions)
['version-1.10', 'version-1.11', 'version-1.9', 'version-2.0']
>>> natsorted(versions)
['version-1.9', 'version-1.10', 'version-1.11', 'version-2.0']
Human Sorting where non-numeric characters are also ordered based on their meaning, not on their ordinal value.
Using the local decimal separator most common en_US.UTF-8.
>>> from natsort import humansorted
>>> 
>>> fruits = ['apple', 'apple1,4', 'Banana9,6', 'apple1,0', 'banana7,7']
>>> sorted(fruits)
['Banana9,6', 'apple', 'apple1,0', 'apple1,4', 'banana7,7']
>>> natsorted(fruits)
['Banana9,6', 'apple', 'apple1,0', 'apple1,4', 'banana7,7']
>>> 
>>> humansorted(fruits)
['apple', 'apple1,0', 'apple1,4', 'banana7,7', 'Banana9,6']
Skaperen and Yoriz like this post
Reply


Messages In This Thread
sorting with numbers in text - by Skaperen - Jul-17-2021, 11:48 PM
RE: sorting with numbers in text - by DeaD_EyE - Jul-18-2021, 08:14 AM
RE: sorting with numbers in text - by snippsat - Jul-18-2021, 10:14 AM
RE: sorting with numbers in text - by Skaperen - Jul-19-2021, 10:30 PM
RE: sorting with numbers in text - by Skaperen - Jul-19-2021, 10:42 PM
RE: sorting with numbers in text - by Skaperen - Jul-20-2021, 01:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  reformatting text with comma separated numbers Skaperen 4 2,701 May-07-2020, 06:14 AM
Last Post: anbu23

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020