How to sort set values in Python? In Python, you can use the `sorted()`

function to sort a set of values. Sets in Python, by definition, are unordered collections of unique elements. When you sort a set, the result will be a new sorted list, as sets themselves don’t have a specific order. To sort elements in ascending or descending order use `sorted()`

functions. By using this function you can sort the set of values by ascending, descending, and in the custom order. Set in Python is a one-dimensional data structure that will not allow duplicate entries.

## 1. Quick Examples of Sorting Values in Set

If you are in a hurry, below are some quick examples of how to sort elements in ascending or descending order in Set.

```
# Quick examples of sorting values in set
# Consider the set with integers
myset=set({12,32,6,56,78,90,54,67})
# Example 1: Sort the elements in the set.
print("Sorted Set: ",sorted(myset))
# Example 2: Sort set in reverse order.
print("Sorted Set: ",sorted(myset,reverse=True))
# Consider the set with strings
myset=set({"hello","welcome","to","sparkby","Examples"})
# Example 3: Sort the elements in the set.
print("Sorted Set: ",sorted(myset))
# Example 3: Sort elements in custom order
print("Sorted Set: ",sorted(myset,key=len))
```

## 2. Python Sort Set Using sorted()

The sorted() function in python is used to sort the Set of values in ascending or descending order. This method can be used with Python data structures like List, Tuple, and Set. It takes three parameters and returns the sorted elements as a list. By default, the elements in the set are sorted in ascending order.

### 2.1 Syntax of Set sorted()

Following is the syntax of the sorted() function.

```
#sorted() syntax
sorted(myset,key,reverse=False)
```

### 2.2 Parameters of Set sorted()

Following are the parameters of the sorted() function.

`myset`

– The first parameter is the input set.`key`

– The key is the optional parameter that takes a user-defined or inbuilt function such that the elements in the set are sorted based on the function return elements.`reverse`

– The reverse is an optional parameter that will sort the elements in the set in ascending order when it is set to False, If it is set to True, it will sort in Descending order. By default, it is False.

## 3. Python Sort Values in Ascending Order

First, let’s sort the python set of values or elements in ascending order. I will use different examples that cover all parameters separately. In the below example, I have created a Set that holds 8 integers and takes the first argument as a set. This returns a list with the elements in ascending order.

```
# Consider the set with integers
myset=set({12,32,6,56,78,90,54,67})
print("Original Set: ",myset)
# Sort the elements in the set
sorted_list = sorted(myset)
print("Sorted Set: ",sorted_list)
```

Yields below output.

Let’s also sort the set with the string values. This sorts the elements in alphabetical order and returns the result as a list.

In this example, the `sorted()`

function is applied to the set, and it returns a new sorted list. Remember that sets in Python are unordered collections, so the order in the printed set may not match the order in which you initially defined it. The `sorted()`

function produces a sorted list based on the natural order of strings.

```
# Consider the set with strings
myset=set({"hello","welcome","to","sparkby","Examples"})
print("Original Set: ",myset)
# Sort the elements in the set
sorted_list = sorted(myset)
print("Sorted Set: ",sorted_list)
```

Yields below output.

## 4. Sort Values in Descending Order

By using the same `sorted()`

function in Python you can also sort the set of elements or values in descending order. To do so, you need to pass the `reverse`

param with the value `True`

to the function.

```
# Consider the set with integers
myset=set({12,32,6,56,78,90,54,67})
print("Actual Set: ",myset)
# Sort the elements in the set in reverse order.
print("Sorted Set: ",sorted(myset,reverse=True))
# Output:
# Actual Set: {32, 67, 6, 12, 78, 54, 56, 90}
# Sorted Set: [90, 78, 67, 56, 54, 32, 12, 6]
```

You can see that all the elements are sorted in descending order because we specified reverse to True. Let’s see an example with a String.

```
# Consider the set with strings
myset=set({"hello","welcome","to","sparkby","Examples"})
print("Actual Set: ",myset)
# Sort the elements in the set in ascending order.
print("Sorted Set: ",sorted(myset,reverse=True))
# Output:
# Actual Set: {'to', 'Examples', 'sparkby', 'welcome', 'hello'}
# Sorted Set: ['welcome', 'to', 'sparkby', 'hello', 'Examples']
```

## 5. Sort Set in Custom Order

Let’s sort the elements in the set by specifying the `key`

parameter. Here we will specify the in-built method – **len**. So the elements are sorted based on the length of the string.

```
# Consider the set with strings
myset=set({"hello","welcome","to","sparkby","Examples"})
print("Actual Set: ",myset)
# Sort the elements in the set based on length of the string.
print("Sorted Set: ",sorted(myset,key=len))
# Output:
# Actual Set: {'to', 'Examples', 'sparkby', 'welcome', 'hello'}
# Sorted Set: ['to', 'hello', 'sparkby', 'welcome', 'Examples']
```

You can see that all the elements are sorted based on the string length in ascending order.

## Frequently Asked Questions On Python Sort Set

**Can I use the sort() method directly on a set in Python?**

Sets in Python do not have a `sort()`

method because sets are inherently unordered collections. If you want to sort the elements of a set, you can use the `sorted()`

function to obtain a sorted list.

**How does the sorted() function work with sets?**

The `sorted()`

function takes an iterable (like a set) and returns a new sorted list. It does not modify the original set. The resulting list will be sorted based on the natural order of the elements.

**Can I sort a set in descending order?**You can sort a set in descending order by using the `sorted()`

function with the `reverse=True`

argument. This argument reverses the sorting order.

**How do I sort a set of custom objects?**If you have a set of custom objects and you want to sort them, you can use the `key`

parameter of the `sorted()`

function. The `key`

parameter allows you to specify a custom function that extracts a comparison key from each element.

**Why doesn’t my original set change when using sorted()?**

The `sorted()`

function returns a new sorted list and does not modify the original set. If you want to modify the set in-place, you can convert the sorted list back to a set.

## Conclusion

We have seen how to sort the elements in a set using the Python `sorted()`

method. It will return the sorted elements in a list. In our entire article, we explained this method by considering all the parameters. You can also use in-built or custom methods in key parameters. By default, it performs in ascending order, to sort in descending order use `reverse=False`

.

## Related Articles

- Python Sort List Descending
- Python Sort Dictionary by Key
- Python Sort Array Values
- Python Sort List in Reverse Order
- Python Sort List of Numbers or Integers
- Python Sort List Alphabetically
- Sort using Lambda in Python
- How to Sort List of Strings in Python
- How to Sort Dictionary by Value in Python
- Python Sort List of Lists
- How to Sort a List of Tuples in Python