Search code examples
pythonlistsortingcmpalphabetical-sort

Sort list of strings alphabetically


So i have a question, how can i sort this list:

['Pera','mela','arancia','UVA']

to be like this:

['arancia','mela','Pera','UVA']

In the exercise it said to use the sorted() function with the cmp argument.


Solution

  • You need to sort your elements based lowercase representation of the strings:

    sorted(['Pera','mela','arancia','UVA'], key=str.lower)
    

    this will output:

    ['arancia', 'mela', 'Pera', 'UVA']