Read up on some sorting algorithms, such as Bubble Sort (Easy to implement and understand but slow O(n^2) ) or QuickSort much faster but more complex, there are many

Sorting algorithm - Wikipedia,...