Python Forum
sorting alphanumeric values in a human way
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting alphanumeric values in a human way
#1
So simple question, I could make it work in a lambda probably but my experience is limited and I suspect there may already be a built in to do what I need. I have some data that I currently sort using sort like so

data.sort(order=['bin','ref'])

I'm not the one who uses the data, so i am only just finding out it only really works 80% of the time. 'bin' is always an integer, so the sort works great, and that's the critical value. sorting by ref is where it breaks down, as 'ref' is alphanumeric which leaves us to a bad order. ref values are of the format 'A1', 'A4', 'A13', which means when sorted 'A13' comes before 'A4', which is not the desired behavior. is there a built in sort option to do a windows style alphabetic sort, instead of a linux style? or do i need to manually mangle the string and split and sort it that way?

I always prefer to use a built in w/ options than roll my own, because one will always be less buggy than the other, and it's not my code lol
Reply
#2
Python sort doesn't have an order keyword, so I don't understand how this is working for you. Can you include some sample data and show the correct order you want from it? I can interpret "integer" as meaning either a python int or a python str that encodes an integer. Which one is your data?
Reply
#3
Here's one option :

from itertools import groupby

def keyfunction (string) :
	return [int (''.join (group)) if key else ''.join (group) for key, group in groupby (string, str.isdigit)]

test_list = ['A35','A4', 'B3', 'A1', 'A15']

print (sorted (test_list, key = keyfunction)) 
If you can and want to install natsort with pip, here's another option :

import natsort

test_list = ['A35','A4', 'B3', 'A1', 'A15']
print (natsort.natsorted (test_list))
Reply
#4
oh sorry! I confused myself, it is a numpy array (a named array i guess) so I am sorting it by the two named columns. I often use terms incorrectly with these arrays so hopefully I am using it correctly here. the entirety of my sort code is as I said above:
def sort_by_column(data, order=['bin','ref']):
    '''sort data in place based on order list'''

    data.sort(order=order)
'bin' is an actual integer and it sorts it fine by that, it's the second ranking that is sorting, but not sorting the way i want. Does that clarify?


https://numpy.org/doc/stable/reference/g....sort.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Human Sorting (natsort) does not work [SOLVED] AlphaInc 2 1,095 Jul-04-2022, 10:21 AM
Last Post: AlphaInc
  Sorting numerical values provided by QAbstractTableModel BigMan 0 1,333 Jun-04-2022, 12:32 AM
Last Post: BigMan
  List sort - alphanumeric? papsphilip 2 2,402 Oct-05-2019, 09:27 PM
Last Post: papsphilip
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 2,991 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  How to convert Python crawled Bing web page content to human readable? dalaludidu 4 3,322 Sep-02-2018, 04:15 PM
Last Post: dalaludidu
  Batch job from epoch to human time jheeman 6 4,437 Feb-27-2018, 10:53 PM
Last Post: jheeman
  Time Difference in Epoch Microseconds then convert to human readable firesh 4 11,539 Feb-27-2018, 09:08 AM
Last Post: firesh
  Sorting values calculated in python stumunro 4 3,899 Sep-13-2017, 06:09 AM
Last Post: nilamo
  sorting nested dict according to values merlem 6 17,467 Apr-01-2017, 10:01 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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