**Overview python sort**

Today we will see in our kodlogs website how we have to use python sort one list by another.What is Sort?Sorting data inside a computer is a common and widely used process.It means sorting any text or numbers.This setting can be of two types .Ascending and Descending .Ascending means A to Z or 0 to 9 and Descending means Z to A OR 9 to 0.Sorting factors are used on lists,tables,and files .Sort in which two items are compared.

**Syntax**

sorted(*iterable*, key=*key*, reverse=reverse)

**For Exampe**

```
A = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
B = [ 0, 1, 1, 0, 1, 2, 2, 0, 1 ]
```

**OUTPUT**

`["a", "d", "h", "b", "c", "e", "i", "f", "g"]`

**Useing Zip() to sort**

Call zip on the listing to kind via and the listing to sort, In that order. Call sorted on the developed zip object to create a sorted listing of tuples representing pairings between the two lists. With the listing of pairings, use a listing comprehension to create a new listing with the sorted items.

**Solution**

- Zip the two lists.
- Create a new, sorted listing based totally on the zip the use of sorted().
- Using a listing comprehension extract the first factors of every pair from the sorted, zipped list.

**Method**

The reason of zip() is to map a comparable index of a couple of containers so that they can be used simply the use of as a single entity.

Below is the implementation of the above approach:

```
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
CODE
A = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
B = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]
print(sort_list(A,B))
A = ["g", "e", "e", "k", "s", "f", "o", "r", "g", "e", "e", "k", "s"]
B = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]
print(sort_list(A,B))
```

**OUTPUT**

```
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']
['g', 'k', 'r', 'e', 'e', 'g', 's', 'f', 'o']
This is output
```

**second list**

```
B = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]
```

**Example**

```
test_list = [ ('a', 1), ('b', 2), ('c', 3), ('d', 4)]
sort_order = ['d', 'c', 'a', 'b']
print ("The original list is : " + str(test_list))
print ("The sort order list is : " + str(sort_order))
res = [tuple for x in sort_order for tuple in test_list if tuple[0] == x]
print ("The sorted list is : " + str(res))
```

**OUTPUT**

```
The original list is : [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
The sort order list is : ['d', 'c', 'a', 'b']
The sorted list is : [('d', 4), ('c', 3), ('a', 1), ('b', 2)]
This is output
```

**Next example**

```
K = ("b", "g", "a", "d", "f", "c", "h", "e")
T = sorted(K)
print(T)
```

**OUTPUT**

`['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']`

I hope you are understand sort () in python with Kodlogs.

```
Ascending
{{sort list|asc|2=
* a
* c
* b
}}
a
b
c
Descending
{{sort list|desc|2=
* a
* c
* b
}}
c
b
a
easy method
```