39 — Sorting — putting a list in order
A loaded list arrives in the order it was written, which is rarely the order
you want to show. Python's sort method puts the list in order and sorted()
builds a sorted copy; this guide loads numbers from JSON, sorts them both ways,
and explains the difference between the two.
Steps
Create
numbers.jsonwith one JSON list of numbers:
run it →[5, 2, 9, 1, 7, 3]Guide 30 used the same file; the numbers here are deliberately out of order.
Load the list with
json_loadfrom guide 14:
run it →use file latest numbers = json_load("numbers.json") show f"Loaded {len(numbers)} numbers: {numbers}"Run it; it prints
Loaded 6 numbers: [5, 2, 9, 1, 7, 3].sorted(numbers)returns a NEW list in ascending order and leaves the original untouched:
run it →numbers = [5, 2, 9, 1, 7, 3] ascending = sorted(numbers) show ascending show numbersIt prints
[1, 2, 3, 5, 7, 9]and then[5, 2, 9, 1, 7, 3]— the original stays exactly as it was.numbers.sort()changes the list ITSELF, in place. After the call the variable holds the sorted list and the old order is gone:
run it →numbers = [5, 2, 9, 1, 7, 3] numbers.sort() show numbersIt prints
[1, 2, 3, 5, 7, 9]. This is the "ascending" order — the smallest first, the largest last.Descending order flips it, the largest first.
sorted(numbers, reverse=True)makes a new list without touchingnumbers:
run it →numbers = [1, 2, 3, 5, 7, 9] descending = sorted(numbers, reverse=True) show descendingIt prints
[9, 7, 5, 3, 2, 1].Now the whole report in one file. Save
sorting.nme:
run it →# Sorting: putting a saved list of numbers in order. # Run: nme r sorting # The file numbers.json must exist in the same folder. use file latest numbers = json_load("numbers.json") show f"Loaded {len(numbers)} numbers: {numbers}" show "" ascending = sorted(numbers) show "sorted(numbers) makes a NEW list:" show ascending show "The original list is unchanged:" show numbers show "" numbers.sort() show "numbers.sort() changes the list IN PLACE:" show numbers show "" descending = sorted(numbers, reverse=True) show "Descending order with reverse=True:" show descending show "Smallest: " + str(numbers[0]) show "Largest: " + str(numbers[-1]) show "" show "Sorted list, one number per line:" for n in numbers: show " " + str(n)Run it with the data file present:
nme r sortingLoaded 6 numbers: [5, 2, 9, 1, 7, 3] sorted(numbers) makes a NEW list: [1, 2, 3, 5, 7, 9] The original list is unchanged: [5, 2, 9, 1, 7, 3] numbers.sort() changes the list IN PLACE: [1, 2, 3, 5, 7, 9] Descending order with reverse=True: [9, 7, 5, 3, 2, 1] Smallest: 1 Largest: 9 Sorted list, one number per line: 1 2 3 5 7 9numbers.sort()changed the list for good, which is why the descending line starts from the sorted order and the one-per-line list uses it too.Korean writes the same steps with
파일 사용 최신andjson읽기. The full Korean program is in the Korean guide.
Try it yourself
Change numbers.json to [100, 3, 42, 17] and rerun sorting.nme; every
order updates. Then add numbers.reverse() after the sort and rerun — the list
now runs largest to smallest.
What you learned
sorted(numbers)returns a new ascending list and leaves the original alone.numbers.sort()reorders the list in place; the old order is lost.sorted(numbers, reverse=True)builds a descending copy.- After sorting,
numbers[0]is the smallest andnumbers[-1]the largest. - Ascending means smallest first; descending means largest first.