Let's say I have a set of class C instances. Here is an example.
class C:
pass
a = C()
a.distance = 1
b = C()
b.distance = 2
my_set = set()
my_set.add(a)
my_set.add(b)
I followed this post here, to sort the set of instances based on distance, but I noticed it can be a bit inefficient for my case. I am wondering if there is a more efficient way to perform the sort here?
new_set = sorted(my_set, key=lambda x: x.distance, reverse=True)