Hi all,

I would like to ask. Im Fairuz and the new member for this forum. I having problems to sort the multi dimentional array in python. ie:
[('DcaUiVb_B09398-11', [None]), ('m3sui_R66441-35', []), ('m3sui_r26557-01', []), ('m3sui_R31225-24', []), ('m3sui_r66147-03', []), ('m3sui_R31225-23', []), ('m3sui_R30803-10', [])]

I have the list but im having problems to sort it according to the first word ie: ('m3sui_r66147-03', []). Im new in python code. Is there anyone that can guide me to do the code for this problems?

Thanks in advance.

Recommended Answers

All 5 Replies

l.sort(key=lambda x:x[0])

hi slate,

the l is the list right?

how about the x and [0]?

Yes l is the list. For the sort method lookup the definition for list.sort.
lambda x:x[0] is equivalent to
def f(x): return x[0]

i have this list:

Component List: [('DcaUiVb_B09398-10', 0), ('DcaUiVb_B09398-11', 5), ('m3sui_R66441-35', 0), ('fairuz', 0), ('shukor', 6)]

I would like to sort it based on the second value (which is in RED) in reverse order. i tried to do List.sort() but it didnt works.

anyone can give me idea? Thanks!

Define a score function for the list items, and sort the list according to this score function

# python 2 and 3
def score(item):
    return int(item[1])

mylist = [('DcaUiVb_B09398-10', 0), ('DcaUiVb_B09398-11', 5), ('m3sui_R66441-35', 0), ('fairuz', 0), ('shukor', 6)]
print(sorted(mylist, key=score, reverse=True))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.