Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting dicts (contained in lists) alphanumerically in Python

I'm having an issue with sorting a list that contains a dict. Currently I am sorting it by a key called 'title' with the following line:

list.sort(key=operator.itemgetter('title'))

The problem with this is that some of my data gets sorted looking like this:

title_text #49
title_text #5
title_text #50

How would I go about sorting it in a way that is more friendly for human consumption while still maintaining the title sort?

like image 764
wierddemon Avatar asked Dec 06 '25 07:12

wierddemon


1 Answers

You are looking for human sorting.

import re
# Source: http://nedbatchelder.com/blog/200712/human_sorting.html
# Author: Ned Batchelder
def tryint(s):
    try:
        return int(s)
    except:
        return s

def alphanum_key(s):
    """ Turn a string into a list of string and number chunks.
        "z23a" -> ["z", 23, "a"]
    """
    return [ tryint(c) for c in re.split('([0-9]+)', s) ]

def sort_nicely(l):
    """ Sort the given list in the way that humans expect.
    """
    l.sort(key=alphanum_key)

data=[
    'title_text #49',
    'title_text #5',
    'title_text #50']
sort_nicely(data)
print(data)
# ['title_text #5', 'title_text #49', 'title_text #50']

Edit: If your data is a list of dicts then:

data=[{'title': 'title_text #49', 'x':0},
      {'title':'title_text #5', 'x':10},
      {'title': 'title_text #50','x':20}]

data.sort(key=lambda x: alphanum_key(x['title']))
# [{'x': 10, 'title': 'title_text #5'}, {'x': 0, 'title': 'title_text #49'}, {'x': 20, 'title': 'title_text #50'}]
like image 119
unutbu Avatar answered Dec 07 '25 19:12

unutbu