2 views
in Python

At the point when I have a list of integers and letters, I need to sort these components by some request. For instance,

ex_array = [1, 3, 2', 2, 1', 3', 3]

This should be sorted as:

sorted(ex_array) = [1', 1, 2', 2, 3', 3, 3]

In case, the order of the letters is given by,

1' < 1 < 2' < 2 < 3' < 3.

I can also make a code when comprising of just integers. Be that as it may, I don't have a clue how to contrast the symbols with primed with unprimed.

Would you be able to guide me in Python?

by (26.4k points)

Sort by the integer value of every component, just as whether that component is an integer:

>>> a = [1, 3, '2', 2, '1', '3', 3, '10', 10, 'potato', "1'", lambda: None, '-3', -3, -4]

>>> sorted(a, key=lambda x: (int(''.join(v for i,v in enumerate(str(x)) if v.isdigit() or (i==0 and v=='-')) or 0), isinstance(x, int)))

[-4, '-3', -3, 'potato', '1', "1'", 1, '2', 2, '3', 3, 3, '10', 10, <function <lambda> at 0x000000DAD51DB9D8>]

On the off chance that two components have a similar integer value, the string will be set before the integer.

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.