-3

I got this assignment for school. The task is: which of the componist comes first in alphabetical order using Python 3. I tried to look up online but couldn't find anything, would appreciate some help. :)

Componists: 'Berlioz', 'Borodin', 'Brian', 'Bartok', 'Bellini', 'Buxtehude', 'Bernstein'

Thanks in advance.

  • Does this answer your question? [How to sort a list of strings?](https://stackoverflow.com/questions/36139/how-to-sort-a-list-of-strings) – Martial P Sep 07 '20 at 07:50

2 Answers2

0

You can use comparison operators to check for the alphabetical order in strings, too: 'Berlioz' < 'Borodin'will return True, while 'Berlioz' > 'Borodin'will return False

Since the list.sort() method uses comparison of the list's components, it is as simple as this:

composers = ['Berlioz', 'Borodin', 'Brian', 'Bartok', 'Bellini', 'Buxtehude', 'Berns
tein']
composers.sort()
print(composers)
Franz
  • 357
  • 3
  • 11
  • [Hasan Bhagat](https://stackoverflow.com/a/63773324/13726558) beat me by a few seconds ;-) The only difference is that his answer creates a new list, while mine does in-place sorting. – Franz Sep 07 '20 at 07:51
-1

If you store the names of the componist in a list and use the sorted function, you can get your sorted list

componist=list(['Berlioz', 'Borodin', 'Brian', 'Bartok', 'Bellini', 'Buxtehude', 'Bernstein'])
sorted_componist=sorted(componist)
print(sorted_componist)

This prints ['Bartok', 'Bellini', 'Berlioz', 'Bernstein', 'Borodin', 'Brian', 'Buxtehude']

Hasan Bhagat
  • 395
  • 1
  • 8