Use the composition str.split , reversed and tuple to create a key function to use with sorted :
sizes = ['285/30/18', '285/30/19', '235/40/17', '315/25/19', '275/30/19'] s = sorted(sizes, key=lambda z: tuple(reversed([int(i) for i in z.split("/")])))
A sorted function takes a sequence and a key function and returns a list of elements in the sequence, sorted by the return value of the key function for each element of the list. This key lambda z function first splits the element into a "/" character to provide a list of strings, which are then converted to numbers, which are then passed to the reversed function, which gives the iterator a reverse order of the accepted sequence (NOTE: this is not yet evaluated), and the function tuple evaluates the inverse iterator, turning it into a sequence that can be used for sorting.
Thus, a sequence of strings formatted as "a / b / c" will be returned sorted by (c, b, a). This leads to:
>>> print s ['235/40/17', '285/30/18', '315/25/19', '275/30/19', '285/30/19']
source share