-1

I'm trying to sort following things

l = ["125.13-ac35", "142.1532-afe354eqd", "125.13-abe319ej", "142.1523-d315aw"]

like this using Python.

l = ["125.13-abe319ej", "125.13-ac35",  "142.1523-d315aw", "142.1532-afe354eqd"]

Someone suggested me to use radix sort, but that doesn't work as I wanted. For example, to sort those things, following list

a = [15, 3216, 120, 1, 1000, 253]

should be sorted like this

a = [1, 1000, 120, 15, 253, 3216]

Is there any sorting algorithm to sort like that?

Peter
  • 49
  • 7
  • The duplicate is wrong. This question is not a duplicate of [Is there a built in function for string natural sort?](https://stackoverflow.com/questions/4836710/is-there-a-built-in-function-for-string-natural-sort). In fact, it is asking for the exact opposite - how to **avoid** sorting numbers in increasing order. Instead, it is a duplicate of [How to sort integers alphabetically?](https://stackoverflow.com/questions/44835964/how-to-sort-integers-alphabetically). – Stef Oct 27 '20 at 12:05

1 Answers1

1

you need to sort the list by converting the inner element to string

a = [15, 3216, 120, 1, 1000, 253]
a.sort(key=lambda x:str(x))

print(a)

output

[1, 1000, 120, 15, 253, 3216]
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • I know I'm not allowed to say thanks in comments, but that was a quick and good answer. works like a charm, thank you so much – Peter Oct 27 '20 at 11:40
  • thanks, you are allowed to say – sahasrara62 Oct 27 '20 at 11:42
  • You can write directly `a.sort(key=str)`. No need for an extra layer of lambda. – Stef Oct 27 '20 at 11:58
  • The comment text box says "Avoid comments like "+1" or "thanks"" because comments like "+1" or "thanks" don't add anything and [should not be posted](https://meta.stackoverflow.com/q/258004/1364007). – Wai Ha Lee Oct 27 '20 at 14:55
  • (also see https://stackoverflow.com/help/privileges/comment fore more) – Wai Ha Lee Oct 27 '20 at 16:14