def base58_decode(s):
base58_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
base58_dict = {c: i for i, c in enumerate(base58_alphabet)}
num = 0
for char in s:
num = num * 58 + base58_dict[char]
return num
def base58_sort(strings):
sorted_strings = sorted(strings, key=base58_decode)
return sorted_strings
def reverse_base58(s):
base58_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
base58_reversed = base58_alphabet[::-1]
return s[::-1].translate(str.maketrans(base58_alphabet, base58_reversed))
def base58_sort_reverse(strings):
reversed_strings = [reverse_base58(s) for s in strings]
sorted_reversed_strings = sorted(reversed_strings, key=base58_decode)
sorted_strings = [reverse_base58(s) for s in reversed(sorted_reversed_strings)]
return sorted_strings
entries = [
{"id": "13zb1hQohfCP69A11HEd6bUwEJWvDQ8aHA", "pvk": "27C5509CF89ACD536"},
{"id": "13zb1hQ328hrrjb1JZyTSvCJeA6G2Cnimh", "pvk": "27C5509D2A263EAEC"},
{"id": "13zb1hQKLKLpgeQNoNe5iD6THhWoi3MSA2", "pvk": "27C5509D3DAB22789"},
{"id": "13zb1hQDg5BwD76c6WEQY9N23a3Gp7vdtk", "pvk": "27C5509D72109FE4C"},
{"id": "13zb1hQ5o248GixbSR9CWpYMZWpGqs5czf", "pvk": "27C5509DE14211B94"},
{"id": "13zb1hQMNQs2BXhnEFJNWCNbkVcJx5fi1q", "pvk": "27C5509DE6884BE68"},
{"id": "13zb1hQSNYa6yFX9JLKfWNHUpq5nMc66Jo", "pvk": "27C5509DE6CD5BA07"},
{"id": "13zb1hQ3sBDHm5cBXPPgfPhYSkzraUcisL", "pvk": "27C5509E54C3DAC86"},
{"id": "13zb1hQemkdPJTbNesEL8G9QkVxuofeSRe", "pvk": "27C5509E5AB85CA8D"},
{"id": "13zb1hQ2Lt3TfWwFcDriNHmjpSoAuENxb3", "pvk": "27C5509E6653BB2C6"},
{"id": "13zb1hQkDBswGYeUhy6rRURgGgDL3fKg72", "pvk": "27C5509EBAEAB9C3C"},
{"id": "13zb1hQZk7o21ahd1MSRtz1ExTxc8BwXBW", "pvk": "27C5509ECE23AD0C2"},
{"id": "13zb1hQzzSib25bt5DbkBFjHmbrHLRR9F2", "pvk": "27C5509F044114CC6"},
{"id": "13zb1hQZtP6vRzDgs2ocKgykwDq2GAnhaQ", "pvk": "27C5509F23F402A18"},
{"id": "13zb1hQ3KwD3xmzaBoZ7PKP1R3XvUYQAmT", "pvk": "27C5509F77598CB93"},
{"id": "13zb1hQxk5UHQDGTNScqWQj1Za9yDshWgH", "pvk": "27C5509F85758D690"},
{"id": "13zb1hQucYX4o6Ep9G9fiCuRpmy7niQ5ya", "pvk": "27C5509FC04638BCA"},
{"id": "13zb1hQMzapKyNLCLjYRMHUJ5h8TGWBwec", "pvk": "27C5509FED4533B7B"},
{"id": "13zb1hQCKJSp9b9dLzb8eJvrBCzS8S3udn", "pvk": "27C550A01109FF928"},
{"id": "13zb1hQxH5MmfWRSbWgU6GWN5Xy72AujDc", "pvk": "27C550A0932A0A20E"},
{"id": "13zb1hQZ9J2eJy68duKVXRZ8x1U8z5eeRe", "pvk": "27C550A10E5968BD3"},
{"id": "13zb1hQAvmfmrg2vpQKr2TyNubtkqzbXGM", "pvk": "27C550A15976F0824"},
{"id": "13zb1hQe4QqeCcAgdCpBjwZbnZtLCtZRdK", "pvk": "27C550A1A30300395"},
{"id": "13zb1hQD8nsQ8EvAcQniHDfZcztGQohEvN", "pvk": "27C550A1B501E37E3"},
{"id": "13zb1hQGhdvbj18u4cwCJ9sz1VKxAZnf1J", "pvk": "27C550A1E8351A31A"},
{"id": "13zb1hQrwq2mL7vhPwUcGSK7rxW88A7UBd", "pvk": "27C550A1EFCB0464A"},
{"id": "13zb1hQxdgGFJtkvwkyw9CK2YdoFqcoHNQ", "pvk": "27C550A2316CED355"},
{"id": "13zb1hQK5D1FQRSuj5Ra7TVA2XbmD1ugiV", "pvk": "27C550A23EBD33204"},
{"id": "13zb1hQedfpdEHcKHX3kG8YwEytDjrBi5Q", "pvk": "27C550A26AA09A051"},
{"id": "13zb1hQKCMAhRN1Zi2AeeCrkTMKiaG5S4y", "pvk": "27C550A329F5AD42E"},
{"id": "13zb1hQsh6nNfVtKkD8W5Yg2sCpSiKyCjN", "pvk": "27C550A36E6C2808F"},
{"id": "13zb1hQSMBKBKtLHrysfJAzyTYndRAnLU5", "pvk": "27C550A3AF37D157D"}]
input_string=[]
direct={}
for entry in entries:
address=entry["id"]
count=len(address)
input_string.append(address[0:count] )
direct[address[0:count]]=entry["pvk"]
sorted_strings = base58_sort(input_string)
sorted_strings_reversed = base58_sort_reverse(input_string)
print("Sorted normal:")
for s in sorted_strings:
print(s,direct[s])
print("Sorted reversed:")
for s in sorted_strings_reversed:
print(s,direct[s])