Right! And if you’re simply looking to sort them as strings, it’s just as straightforward. The default behavior of sorted()
is to sort elements lexicographically. So, if the elements in your set are already strings, sorted()
will sort them alphabetically. Here’s how it looks:
s = set(['0.000000000', '0.009518000', '10.277200999', '0.030810999', '0.018384000', '4.918560000'])
sorted_s = sorted(s)
print(sorted_s)
Output:
['0.000000000', '0.009518000', '0.018384000', '0.030810999', '10.277200999', '4.918560000']
As you can see, when sorting strings, the order might not be what you expect numerically. If that’s fine for your use case, then this method works perfectly. But for numerical sorting, remember to use key=float
.