I have a set Show
After sorting I want it to look like
Any idea please
SilentGhost 293k64 gold badges301 silver badges291 bronze badges asked Apr 19, 2010 at 16:21
Jeff Atwood talks about natural sort and gives an example of one way to do it in Python. Here is my variation on it:
Use like this:
Output:
One advantage of this method is that it doesn't just work when the strings are separated by spaces. It will also work for other separators such as the period in version numbers (for example 1.9.1 comes before 1.10.0). answered Apr 19, 2010 at 16:31
Mark ByersMark Byers 780k183 gold badges1551 silver badges1440 bronze badges 4 Short and sweet:
This version:
If you want printed output exactly as described in your example, then:
answered Apr 19, 2010 at 17:32
Daniel StutzbachDaniel Stutzbach 71.3k17 gold badges85 silver badges76 bronze badges 3 You should check out the third party library natsort. Its algorithm is general so it will work for most input.
answered Jul 17, 2014 at 22:01
SethMMortonSethMMorton 42.4k12 gold badges64 silver badges80 bronze badges A simple way is to split up the strings to numeric parts and non-numeric parts and use the python tuple sort order to sort the strings.
answered Apr 19, 2010 at 16:37
Ants AasmaAnts Aasma 51.3k15 gold badges88 silver badges92 bronze badges It was suggested that I repost this answer over here since it works nicely for this case also
Demo:
For Python3 it's necessary to modify it slightly (this version works ok in Python2 too)
answered Jun 6, 2013 at 7:35
John La RooyJohn La Rooy 285k51 gold badges358 silver badges498 bronze badges Generic answer to sort any numbers in any position in an array of strings. Works with Python 2 & 3.
Sample:
Part of the answer is from there
answered Oct 5, 2016 at 23:23
Le DroidLe Droid 4,2942 gold badges34 silver badges32 bronze badges
answered Apr 19, 2010 at 16:25
SilentGhostSilentGhost 293k64 gold badges301 silver badges291 bronze badges Based on SilentGhost's answer:
answered Apr 19, 2010 at 16:31
sets are inherently un-ordered. You'll need to create a list with the same content and sort that. answered Apr 19, 2010 at 16:25
RakisRakis 7,65923 silver badges25 bronze badges 3 For people stuck with a pre-2.4 version of Python, without the wonderful
This does not answer the specific question above ( answered Oct 22, 2013 at 16:11
0
answered Apr 1, 2021 at 17:23
tldrtldr 1063 bronze badges How do you sort alphanumeric numbers?To sort, highlight both columns and sort on B. If there can be other prefixes, say b-, c-, etc., similarly split off the text portion to another column. Then sort on the text column as the first sort column and the number column as the second. Everything you highlight will get sorted according to the sort columns.
What is alphanumeric sorting?SORT-TYPES is an alphanumeric property that can be repeated for each column contained in the grid. The value determines the sorting property of the column as shown in the table. Many of these values are similar to those for the DATA-TYPES property.
How do you sort numbers and letters in Python?Python sorted() Function
You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically. Note: You cannot sort a list that contains BOTH string values AND numeric values.
How do you sort numerical data in Python?Let's discuss ways in which this is performed.. Method #1 : Naive Method. In the naive method requires the type conversion of all the elements into integers of the list iterated through a loop. ... . Method #2 : Using sort() using key. ... . Method #3 : Using sorted() + key. ... . Method #4 : Using sorted() + key with len (). |