I have a list of lists like:
myList = [
["name1", [12, 89]],
["name2", [37, 2]],
["name3", [5, 4]],
["name4", [67, 998]]
]
- The first element inside the inner list is the name
- The second element inside the inner list is another list with two numbers
I want to use the sort()
method to sort the list by:
- Name
- number1
- number2
For example, for the first element, sort by:
- "name1"
- 12
- 89
Can this be achieved with lambda functions?
To sort by name I did: myList.sort(key = lambda x: x)
How can I use lambda functions to sort by 2. and 3. (the numbers)
Desired Output
- Sort by "Name" gives:
myList = [
["name1", [12, 89]],
["name2", [37, 2]],
["name3", [5, 4]],
["name4", [67, 998]]
]
- Sort by "Number1" gives:
myList = [
["name3", [5, 4]],
["name1", [12, 89]],
["name2", [37, 2]],
["name4", [67, 998]]
]
- Sort by "Number2" gives:
myList = [
["name2", [37, 2]],
["name3", [5, 4]],
["name1", [12, 89]],
["name4", [67, 998]]
]