I am in the process of creating a bot that will compare the price of many items. In the list I have the price with some other information. However, I would like to sort e.g. this list
['Soul String', 'RARE', [900000, '3b4c1f7f9ae447a983817c303b62375a', 'Soul String'], [4000000, 'feacbc053bb240be954a4b17c14077d2', 'Soul String'], [4000000, 'feacbc053bb240be954a4b17c14077d2', 'Soul String'], [700000, '01b6b2ec8a5c4a4e934149a33e99802e', 'Soul String'], [700000, 'be1aa3a0700b403898a2e6e05d77e63d', 'Soul String']]
by the price, so it looks like this list
['Soul String', 'RARE', [4000000, 'feacbc053bb240be954a4b17c14077d2', 'Soul String'], [4000000, 'feacbc053bb240be954a4b17c14077d2', 'Soul String'], [700000, '01b6b2ec8a5c4a4e934149a33e99802e', 'Soul String'], [700000, 'be1aa3a0700b403898a2e6e05d77e63d', 'Soul String']], [900000, '3b4c1f7f9ae447a983817c303b62375a', 'Soul String']
But I have no clue how i can manage this, maybe one of you guys can help me
Advertisement
Answer
You can custom sort via lambda:
l = ['Soul String', 'RARE', [900000, '3b4c1f7f9ae447a983817c303b62375a', 'Soul String'], [4000000, 'feacbc053bb240be954a4b17c14077d2', 'Soul String'], [4000000, 'feacbc053bb240be954a4b17c14077d2', 'Soul String'], [700000, '01b6b2ec8a5c4a4e934149a33e99802e', 'Soul String'], [700000, 'be1aa3a0700b403898a2e6e05d77e63d', 'Soul String']] sorted(l, key=lambda x: x[0] if type(x[0])==int else 999999999, reverse=True) ['Soul String', 'RARE', [4000000, 'feacbc053bb240be954a4b17c14077d2', 'Soul String'], [4000000, 'feacbc053bb240be954a4b17c14077d2', 'Soul String'], [900000, '3b4c1f7f9ae447a983817c303b62375a', 'Soul String'], [700000, '01b6b2ec8a5c4a4e934149a33e99802e', 'Soul String'], [700000, 'be1aa3a0700b403898a2e6e05d77e63d', 'Soul String']]
Please note that your current format is not ideal, because you are mixing the data. You could consider using a dictionary like this:
d = {'Soul String': {'name': 'Soul String', 'rarity': 'RARE', 'stock': [[900000, '3b4c1f7f9ae447a983817c303b62375a', 'Soul String'], [4000000, 'feacbc053bb240be954a4b17c14077d2', 'Soul String'], [4000000, 'feacbc053bb240be954a4b17c14077d2', 'Soul String'], [700000, '01b6b2ec8a5c4a4e934149a33e99802e', 'Soul String'], [700000, 'be1aa3a0700b403898a2e6e05d77e63d', 'Soul String']]}} d['Soul String']['stock'] = sorted(d['Soul String']['stock'], key = lambda x: x[0], reverse = True)