sort an array in python

To sort an array in Python, you can use the built-in sorted() function. The sorted() function takes an iterable as an argument, and returns a new sorted list in ascending order.

Here's an example of how to use sorted() to sort an array:

main.py
arr = [4, 1, 3, 2, 5]
sorted_arr = sorted(arr)
print(sorted_arr)
65 chars
4 lines

Output:

main.py
[1, 2, 3, 4, 5]
16 chars
2 lines

Alternatively, you can use the sort() method of a list to sort it in place:

main.py
arr = [4, 1, 3, 2, 5]
arr.sort()
print(arr)
44 chars
4 lines

Output:

main.py
[1, 2, 3, 4, 5]
16 chars
2 lines

related categories

gistlibby LogSnag