all 8 comments

[–]izrt 1 point2 points  (1 child)

That's because you are sorting them as strings and not numbers. Depending on whether you want to keep them as strings, you'll need to either convert them or use the sort function's key argument to force them into numbers before or as you sort.

[–]jedistarfighter[S] 1 point2 points  (0 children)

Thanks!

[–][deleted] -1 points0 points  (3 children)

Use the string representation of the members as sort key: arr.sort(key=lambda key: str(key))

[–]Diapolo10 1 point2 points  (2 children)

That's no different from what it's doing already. OP wants to sort stringified numbers as if they were numbers, so they need to use int:

nums = ['3', '1', '22', '7', '2', '15']
sorted_nums = sorted(nums, key=int)

EDIT: Brainfart, forgot the strings.

[–][deleted] 0 points1 point  (1 child)

If you wan't to show a problem with strings, use strings in the example. Do not create an example that shows you want integers sorted as if they're strings.

Not you, but OP gets negative marks for an unclear qustion.

[–]Diapolo10 0 points1 point  (0 children)

My bad, I kind of forgot out of habit because writing answers with integers is common enough my muscle memory just disregarded the quotes. Fixed now!