US Trends

what is the correct sorting function to list in alphabetical order

The correct function depends on the language, but for Python the usual choice is sorted() or the list method .sort() for alphabetical order.

Quick answer

  • sorted(my_list) returns a new list in alphabetical order.
  • my_list.sort() sorts the existing list in place.

Example

python

names = ["banana", "apple", "cherry"]

print(sorted(names))   # ['apple', 'banana', 'cherry']
names.sort()
print(names)           # ['apple', 'banana', 'cherry']

If you meant JavaScript

JavaScript uses array.sort(), and for proper alphabetical sorting of strings it is common to use a compare function or lowercase comparison.

Short version

If you just want the alphabetical sort function in Python, use sorted() or .sort().