0

How can I sort some objects on the following list with the result of the function?

sorted_list = []
for c in objects:
    sorted_list.insert(c)

    sorted_list.sort(function(c))

Something like a map

Example:

objects = [node3, node2, node1, node4]

def function (node, player):
   if (node == node1): return 1
   else if (node == node2): return 2
   else if (node == node3): return 3
   else if (node == node4): return 4

output:

objects = [node1, node2, node3, node4]
Bny4W
  • 21
  • 5
  • 1
    Possible duplicate of [Custom Python list sorting](https://stackoverflow.com/questions/11850425/custom-python-list-sorting) – Jongware Dec 12 '18 at 22:52

1 Answers1

1

Use the ranking function as the sorting key:

sorted_list.sort(key=lambda node: function(node, player))

I don't know why you pass player as parameter if you're not using it. If you remove it, the key will be even simpler:

sorted_list.sort(key=function)
Óscar López
  • 232,561
  • 37
  • 312
  • 386