Sort short_names in reverse alphabetic order [SOLVED]

In this article we will study how we can  sort short_names in reverse alphabetic order in python using built in functions with a little modification to them.

 

How to sort short_names in reverse alphabetic order

Method-1: Sort and Reverse

First method of doing so is to reverse the array after sorting the array.

Advertisement

The code will be

# sort short_names in reverse alphabetic order.
short_names = ['Jan', 'Sam', 'Ann', 'Joe', 'Tod']
short_names.sort()
# reverse the array after sorting
res = short_names[::-1]
print(" sort short_names in reverse alphabetic order is :",res)

The output of this code is :

sort short_names in reverse alphabetic order is : ['Tod', 'Sam', 'Joe', 'Jan', 'Ann']

 

Method-2: Built in functions

Python has an in built sort function but that would return the array in an alphabetic order. Eg.

# sort short_names in reverse alphabetic order.
short_names = ['Jan', 'Sam', 'Ann', 'Joe', 'Tod']
short_names.sort()
print(" sorted array is :",short_names)

The output for this is :

 sorted array is : ['Ann', 'Jan', 'Joe', 'Sam', 'Tod']

To fix this, we will use the reverse option. Python has a reverse option

sort(*, key=None, reverse=None)

This method sorts the list in place, using only < comparisons between items.reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

Advertisement

Using this the code becomes :

# sort short_names in reverse alphabetic order.
short_names = ['Jan', 'Sam', 'Ann', 'Joe', 'Tod']
short_names.sort(reverse=True)
print("sort short_names in reverse alphabetic order is :",short_names)

The output of this code is :

sort short_names in reverse alphabetic order is : ['Tod', 'Sam', 'Joe', 'Jan', 'Ann']

So the array now is in reverse alphabetical order.

 

The Python Sort() Function

The python sort function allows sorting of a list in an ascending or descending order.  The function takes two arguments, key and reverse.

List.sort(key=..,reverse=..)

The key is for sort comparison and reverse is to reverse order the list, it depends either you want the list in ascending or or descending order. Eg.

 

Ascending order sort

sortarr=['a','e','f','g','s','t','k','p','m']
sortarr.sort()
print("list in ascending order ",sortarr)

The output of this code is :

Advertisement
list in ascending order  ['a', 'e', 'f', 'g', 'k', 'm', 'p', 's', 't']

 

Descending order sort

sortarr=['a','e','f','g','s','t','k','p','m']
sortarr.sort(reverse=True)
print("list in ascending order ",sortarr)

The output of this code is :

list in descending order  ['t', 's', 'p', 'm', 'k', 'g', 'f', 'e', 'a']

 

Conclusion

In this article  we learned how to sort short_names in reverse alphabetic order. First was the naive approach, to sort the array and then reverse it. The second one is also similar but in that case we used in built function of sort and passed it an argument to reverse the pattern. This way we were able to  sort short_names in reverse alphabetic order. We also looked through the python sort function. How it works and what are its applications.

 

Further Reading

Sort short_names in reverse alphabetic order

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X