## DEV Community 👩‍💻👨‍💻 is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

AbreuY

Posted on

# [Python] Sort a list - Hard & Easy way

Sorting a list in ascending and descending order.

Hard way!

``````arr = [5,2,8,7,1]
temp = 0
for i in range(0,len(arr)):
for j in range(i+1, len(arr)):
if arr[i] > arr[j]:
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp

print("Array sorted in ascending order: ")

for i in range(0, len(arr)):
print(arr[i], end=" ")

for i in range(0,len(arr)):
for j in range(i+1, len(arr)):
if arr[i] < arr[j]:
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp

print("", end="\n")

print("Array sorted in descending order: ")

for i in range(0, len(arr)):
print(arr[i], end=" ")

``````

Easy way!

``````arr.sort()
print("", end="\n")
print("Array sorted in ascending order: ")
print(arr)

print("", end="\n")
print("Array sorted in descending order: ")
arr.sort(reverse=True)
print(arr)
``````

## Take a look at this:

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠