You define partially an order for the case in which the last two figures are equal, because you do not indicate how to order the elements within that case (should it appear before 0512 or 0612? With what criteria?).
On the other hand, for the case in which the last two figures do not coincide, there is no ordering criterion. I am not sure if this indicates that it does not matter in which order it appears, or if on the contrary, the relative order in which they appeared in the original sequence should be specified.
There is a case with a very simple solution, and it is the one that I am going to solve in case it serves you. It is about considering the last two figures as if they were a month, so to speak, and the first two as if they were the day, so to speak, and then ordering all the elements in a "chronological" order (so to speak).
With this criterion, 0512 would go before 0612. But 0208 should move to the beginning of the list (since 08 would be the smallest "month").
This is implemented by providing
sorted() with a function
key that puts the last two characters of the string ahead, and let
sorted do the rest. So:
sorted(lista, key=lambda x: x[-2:] + x[:-2])
['0208', '0312', '0512', '0612', '0113', '0716', '0916', '0827', '0430']
The case in which you must preserve the original order of the elements that do not end the same is not really an order (the title of the question is equivocal), but rather a "grouping". We want to simply group, in the same order in which they appear in the list, the elements that end the same.
This can be done with a dictionary whose key is a two-digit string (ex: "12") and whose value is a list with all the elements that end up like this, in the order in which they appear. There will be elements of the dictionary (for example, the one with a "13" key) whose list would have a single element. We also want the dictionary to preserve the order in which we are inserting the keys, because it is the order in which we will find them in the original list.
A normal python dictionary does not preserve the order (unless you use python 3.6 or higher), but you can use a
This would be the proposed solution:
from collections import OrderedDict
# Primero creamos el diccionario con las agrupaciones
grupos = OrderedDict()
for dato in l:
cifras_finales = dato[-2:]
if cifras_finales in grupos:
grupos[cifras_finales] = [dato]
# Ahora usamos ese diccionario para crear la lista final a retornar
resultado = 
for v in grupos.values():
['0113', '0208', '0312', '0512', '0612', '0430', '0716', '0916', '0827']