python custom sort

data Generator

from random import randint

def rnd_3_char():
    return chr(65+randint(0,25))+chr(65+randint(0,25))+chr(65+randint(0,25))

def gen_data():
    A=['C1','C2','C3','T1','T2', 'T3']
    B=['3s','4s','5s','6m','1h','8h','24h']
    return "{}_{}_{}".format(rnd_3_char(),A[randint(0,len(A)-1)],B[randint(0,len(B)-1)])

data=[gen_data() for a in range(500)]

This looks like

['YTI_T1_5s',
 'ZHB_T2_8h',
 'RRN_C3_6m',
 'VLW_C1_4s',
 'AUP_T3_6m',
 'OFU_T1_4s',
 'XTE_C2_5s',
 'VQV_T3_8h',
 'NIC_C3_4s',
 'RLC_T2_8h']

Custom Sort

This is my Custom Sort

import re

def my_sort(a):
    sec_cnt=0
    parts=a.split('_')
    match=re.findall('([0-9]+)([shm])',parts[2])
    try:
        if match[0][1]=='s':
            sec_cnt=int(match[0][0])
        elif match[0][1]=='m':
            sec_cnt=60*int(match[0][0])
        elif match[0][1]=='h':
            sec_cnt=3600*int(match[0][0])
    except:
        #print("{}".format(parts[1]))
        pass
    #print("{} {}".format(parts[1],sec_cnt))
    return parts[1],sec_cnt

And using it

data_sorted=sorted(data,key=my_sort)

Another way