AnsweredAssumed Answered

How can I naturally sort alphanumeric values in a string?

Question asked by rblash on Jul 28, 2016
Latest reply on Aug 1, 2016 by rblash

My field had values like B3, M4, S5, E26, E10, E9, E11 (all within one field).


I need to naturally sort so the values look like this: B3, E9, E10, E11, E26, M4, S5


I've tried the following code as suggested here, but it gives me an error stating "The value type is incompatible with the field type". If anyone has some suggestions I'd be interested! BTW I used this in Field Calculator


import re


def sorted_nicely( l ):

    """ Sorts the given iterable in the way that is expected.


    Required arguments:

    l -- The iterable to be sorted.



    convert = lambda text: int(text) if text.isdigit() else text

    alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]

    return sorted(l, key = alphanum_key)