-2

In data analysis we get a situation like a data is 1.1 and another like 1.1.2 where we have to sort the data

but it cannot understand the second data as a number it only looks for first number. Is there a hack to sort this data...

  • What type is `1.1` in this case? – Holloway Dec 05 '22 at 16:16
  • Please provide a more extensive input example with desired output if you expect a useful answer. For now, you can try searching for 'Python version sorting'. – dawg Dec 05 '22 at 16:19
  • 1
    Try `sorted(['1.1', '1.0.10' '1.2', '1.0', '1.10.01'], key=lambda s: [int(e) for e in s.split('.')])` – dawg Dec 05 '22 at 16:24

1 Answers1

2

Use a lambda with sorted, splitting on '.' and mapping each section to int.

>>> lst = ["1.1.2", "3.4.5", "1.1"]
>>> sorted(lst, key=lambda x: [int(y) for y in x.split('.')])
['1.1', '1.1.2', '3.4.5']
>>>
Chris
  • 26,361
  • 5
  • 21
  • 42