Reputation: 1220
I'm a little lost. I want to sort a list, that has the format of :
[instance_of_myClass, instance_of_myClass, instance_of_myClass...]
init
of myClass
looks like this:
def __init__(self):
self.ip = ""
self.hostname = ""
self.further_informtion
I want it to be sorted by it's IP adress, which I found as
sorted(list_of_ips, key=lambda ip: long(''.join(["%02X" % long(i) for i in ip.split('.')]), 16))
from How to sort IP addresses stored in dictionary in Python?
I have trouble transfering it though. Can someone help me with this?
Upvotes: 1
Views: 115
Reputation: 99
[instance_of_myClass, instance_of_myClass, instance_of_myClass...].sort(lambda a, b: cmp(a.ip, b.ip))
Upvotes: 0
Reputation: 1518
according to the doc:
key specifies a function of one argument that is used to extract a comparison key from each list element. The default value is None.
so the actual argument to your above lambda function you provided as the key is the instances of your 'myClass', so in order to sort your class instances by ip, you should write sth like:
key=lambda my_instace: long(''.join(["%02X" % long(i) for i in my_instance.ip.split('.')]), 16))
Upvotes: 1