-1

I have a problem where I couldn;t find a way to sort a list of strings in such a way that the values that starts a digit sorted in the end, and value that starts with letters are at the start of the list

I have tried using sort and sorted methods with lambda but its always sort with numbers at the start of the list.

Here is what I have tried

if __name__ == "__main__":
    l = ["9 1 7 2", "hell bag", "8 7", "deserve respect three", "phone exit retired"]
    print(sorted(l))

and this gives me following output,,

['8 7', '9 1 7 2', 'deserve respect three', 'hell bag', 'phone exit retired']

I want following output instead,

['deserve respect three', 'hell bag', 'phone exit retired', '8 7', '9 1 7 2']
foo_paul
  • 31
  • 3
  • Possible duplicate of: https://stackoverflow.com/questions/11850425/custom-python-list-sorting – ma3oun Oct 26 '19 at 09:31
  • I am sorry but i don't see any duplication between the two? I have already tried `lambda` with `key` but it allows me to provide what key to use for sorting but not `alpha` or `numeric` – foo_paul Oct 26 '19 at 09:34
  • So show a [mcve] of that. What do you think is the limitation of key that means you can't implement what you describe? – jonrsharpe Oct 26 '19 at 09:40

1 Answers1

0

You can use reversed() method to get your target result, like this: print(list(reversed(sorted(l))))

output is: ['phone exit retired', 'hell bag', 'deserve respect three', '9 1 7 2', '8 7']