“Without changing the original order of the data set, the sorted() method returns a set of items that have been arranged in order. The “key” additional argument of the sorted() method accepts a method as its value. Before sorting, this parameter modifies every component; it receives the value as well as returns 1, which is subsequently utilized in place of the actual value. Let’s talk about the sorted() function in detail.”
Example no 1
In this instance we will utilize list data type, tuple data set and string to sort the data.
a_nums = sorted(n_1)
d1_nums = sorted(n_1, reverse = True)
print("The Ascending Value: ", a_nums)
print("The Descending Value: ", d1_nums)
n_2 = (85,7,76,190,91)
asc_nums1 = sorted(n_2)
dsc_nums1 = sorted(n_2, reverse = True)
print("The Ascending Value: ", asc_nums1)
print("The Ascending Value: ", dsc_nums1)
mystr_1 = 'gucafdbdfke'
asc_str2 = sorted(mystr_1)
dsc_str2 = sorted(mystr_1, reverse = True)
print("The Ascending Value: ", asc_str2)
print("The Reversed String value: ", dsc_str2)
We will start the code by initializing the variable “n_1”. Here we store the random values in the form of a list. Then we utilize the function sorted(). This function will arrange the list in ascending order. We pass that defined list as the parameter of this function as we want to sort the above-mentioned list.
In the next step, we declare a new variable named “d1_nums”. Along with this, we call the sorted() method once again, and here we will give the “reverse” as the argument of this function. By doing so, the function sorted() will sort the values of the list in descending order. Then we employ the print() function. The first print statement shows the elements of the list in ascending format, and the second print statement prints the values in descending order.
Now we will declare some values and store those values in the form of a tuple. The variable used to store the tuple is “n_2”. Next, the method sorted () is used. The values of the tuple will be formatted using this method in ascending order. This function receives the required tuple as an argument. Considering that we want to arrange the aforementioned tuple. The following step is declaring a new variable called “dsc_nums1.”
We were using the sorted() function yet again, and this time we will be passing it the parameter “reverse.” This argument’s value would be modified as “True.” now, the tuple’s items will have been arranged in descending order by the sorted() method. Afterward, we call the print() method. The entries of the tuple are displayed in ascending order by using the first print() method, while the values are printed in descending order with the help of the second print() method.
In the last portion of the code, we want to sort the random string. Firstly, we store the string in the variable “mystr_1”. Then we apply the sorted() method twice. The first sorted() method is being used to arrange the characters of the string in ascending order. That sorted string is stored in a variable “asc_str2”. Then we employ another sorted() method. By using this function, the string would be sorted in a reversed order. Here this function contains the “reverse” argument. Lastly, we have applied the print() function to show strings in both ascending and reversed formats.
Example no 2
Here we will sort the data of the dictionary data type.
asc_nums1 = sorted(n_1)
dsc_nums1 = sorted(n_1, reverse=True)
print("Here is Ascending List: ", asc_nums1)
print("Here isDescending List: ", dsc_nums1)
The variable termed “n_1” is being initialized. Within this variable, we define values in the form of a dictionary data set. We stored three elements. In addition to this, we declare two new variables, “asc_nums1” and “dsc_nums1”. We apply the sorted() function. The variable “asc_nums1” will store the elements of the dictionary that are sorted in ascending order. We call the sorted() method once again.
We provide the parameter “reverse” to the function sorted(). We specify the value of the “reverse” attribute as “True”. The variable “dsc_nums1” is being used to store the arranged values of the dictionary. To terminate the program, we will employ the print() methods respectively to show the components of the dictionary in ascending order as well as descending order.
Example no 3
In this case, we are going to sort the data by using the “key” attribute.
asc1_nums = sorted(n_1, key=len)
dsc1_nums = sorted(n_1, key=len, reverse=True)
print("Here is Ascending List: ", asc1_nums)
print("Here is Descending List: ", dsc1_nums)
First, the variable known as “n_1” is created. We indicate four different values of any set and store these values in this variable. We also specify two new variables, “asc_nums” and “dsc_nums.” Along with this, we have used the method sorted(). This method takes two parameters, one of which is the defined list and the other of which is the “key” attribute. The items arranged in ascending order would be stored in the variable “asc_nums”.
We will be using the sorted() method. The list, the key attribute, and the reverse attribute are the three arguments of this method. We set the “reverse” attribute’s value to “True”. The ordered elements are kept in the variable “dsc_nums.” We will be using the print() functions to display the elements in ascending order as well as descending order, and this is how we end the program.
Example no 4
We will utilize the last character to arrange the data as well as employ the “lambda” value to the “key” argument.
code1 = ('rbb','cud', 'aua', 'zoc')
asc1_code = sorted(code1, key=getlastchar)
dsc1_code = sorted(code1, key=getlastchar, reverse=True)
print("Here is Ascending Code: ", asc1_code)
print("Here is Descending Code: ", dsc1_code)
print('----Now we will use the lambda function----')
asc2_code = sorted(code1, key=lambda s: s[len(s)-1])
dsc2_code = sorted(code1, key=lambda s: s[len(s)-1], reverse=True)
print("Here is Ascending Code: ", asc2_code)
print("Here is Descending Code: ", dsc2_code)
We are going to start the program by defining the getlastchar() method. This function returns the last value of the defined set. We use the len() function here. This function is being used to find the length of the set. The total length will be subtracted by 1. We define some values that can be accumulated in the “code1” variable. We will sort the data set by using the sorted() function. The defined set will be sorted in ascending and descending order.
Then we utilize the lambda function to sort the elements. Similarly, we call the function sorted() to arrange the values. But now, we set the value of the “key” parameter as “lambda”. The print() function prints the elements.
The python sorted reverse has been covered in this article. The sorted() method returns a data set after ordering the components of an iterable in either ascending or descending order. The data will be arranged in reverse using the reverse = True argument. In this guide, we have sorted the data that are stored in different data types, which include tuples, dictionaries, lists, and strings. We also sorted the data of the set by using the key parameter and the last character of the data set.