{"id":"lists-sorting","slug":"sorting","title":"Sorting: sort and sorted","text":"The **`sort`** method puts the items of a list in order, from smallest to greatest. Strings are sorted in alphabetical order:\n\n```python\nscores = [12, 30, 7, 18]\nscores.sort()\nprint(scores)\nnames = [\"Linus\", \"Ada\", \"Grace\"]\nnames.sort()\nprint(names)\n```\n\n```output\n[7, 12, 18, 30]\n['Ada', 'Grace', 'Linus']\n```\n\n`sort` changes the list itself: the original order is gone. When you still need the original, use the function **`sorted`** instead. It doesn't touch the list you give it, but returns a new, sorted list:\n\n```python\narrivals = [\"Linus\", \"Ada\", \"Grace\"]\nalphabetical = sorted(arrivals)\nprint(arrivals)\nprint(alphabetical)\n```\n\n```output\n['Linus', 'Ada', 'Grace']\n['Ada', 'Grace', 'Linus']\n```\n\nSo `arrivals` still says who came first, and `alphabetical` is a sorted copy.\n\nFor the order from greatest to smallest, sort the list and then turn it around with the **`reverse`** method, which reverses the order of any list:\n\n```python\nscores = [12, 30, 7, 18]\nscores.sort()\nscores.reverse()\nprint(scores)\n```\n\n```output\n[30, 18, 12, 7]\n```\n\n### A common mistake\n\n`sort` changes the list and returns nothing, that is, `None`. Storing what it returns gives you `None` and not the sorted list:\n\n```python\nscores = [12, 30, 7]\nordered = scores.sort()\nprint(ordered)\nprint(scores)\n```\n\n```output\nNone\n[7, 12, 30]\n```\n\nEither sort the list itself with `scores.sort()` and keep using `scores`, or make a sorted copy with `ordered = sorted(scores)`. The same goes for `append`, `insert` and `remove`: they change the list and return `None`, so `scores = scores.append(5)` loses the whole list.\n\n### Capital letters\n\nIn alphabetical order, Python puts every capital letter before every small letter, so `\"Zebra\"` comes before `\"apple\"`:\n\n```pycon\n>>> sorted([\"banana\", \"Zebra\", \"apple\"])\n['Zebra', 'apple', 'banana']\n```\n\nWrite the strings in the same case when you want a normal alphabetical order.\n","task":"The program should print the scores in the order they were given and then from smallest to greatest, but the second line prints `None`. Fix it so it prints:\n\n```\n[12, 30, 7, 18]\n[7, 12, 18, 30]\n```\n","kind":"code","seed":"scores = [12, 30, 7, 18]\nordered = scores.sort()\nprint(scores)\nprint(ordered)\n","solution":"scores = [12, 30, 7, 18]\nordered = sorted(scores)\nprint(scores)\nprint(ordered)\n","hints":["`scores.sort()` sorts `scores` itself and returns `None`, so `ordered` is `None`, and `scores` loses its original order.","Make a sorted copy instead: `ordered = sorted(scores)`. It leaves `scores` as it was."],"tests":[{"name":"Output","stdin":"","expect":"[12, 30, 7, 18]\n[7, 12, 18, 30]","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"words = [\"pear\", \"fig\", \"apple\"]\nother = sorted(words)\nprint(words)\nprint(other)\nwords.sort()\nprint(words[0])\nprint(words.sort())\n","solution":"words = [\"pear\", \"fig\", \"apple\"]\nother = sorted(words)\nprint(words)\nprint(other)\nwords.sort()\nprint(words[0])\nprint(words.sort())\n","hints":["`sorted` returns a sorted copy and leaves `words` as it was.","After `words.sort()`, `words` itself is in alphabetical order.","`sort` returns `None`, and `print` shows it."],"tests":[{"name":"Output","stdin":"","expect":"['pear', 'fig', 'apple']\n['apple', 'fig', 'pear']\napple\nNone","hidden":false}],"require":[],"forbid":[],"lines":["['pear', 'fig', 'apple']","['apple', 'fig', 'pear']","apple","None"]},{"task":"Write a program that reads exam scores (whole numbers) until the user types an empty line. Then it prints the scores in order from lowest to highest, the lowest score and the highest score:\n\n```\nScore: 15\nScore: 9\nScore: 20\nScore:\n[9, 15, 20]\nLowest: 9\nHighest: 20\n```\n\nYou can assume the user types at least one score.\n","kind":"code","seed":"scores = []\n","solution":"scores = []\nwhile True:\n    line = input(\"Score: \")\n    if line == \"\":\n        break\n    scores.append(int(line))\nscores.sort()\nprint(scores)\nprint(\"Lowest:\", scores[0])\nprint(\"Highest:\", scores[-1])\n","hints":["Read each line as a string and stop on `\"\"`; convert the others with `int` before you append them, so they sort as numbers.","After `scores.sort()`, the lowest score is the first item and the highest is the last.","The first item is `scores[0]` and the last is `scores[-1]`."],"tests":[{"name":"Test 1","stdin":"15\n9\n20\n\n","expect":"Score: 15\nScore: 9\nScore: 20\nScore:\n[9, 15, 20]\nLowest: 9\nHighest: 20","hidden":false},{"name":"Test 2","stdin":"7\n18\n3\n11\n100\n\n","expect":"Score: 7\nScore: 18\nScore: 3\nScore: 11\nScore: 100\nScore:\n[3, 7, 11, 18, 100]\nLowest: 3\nHighest: 100","hidden":true},{"name":"Test 3","stdin":"12\n\n","expect":"Score: 12\nScore:\n[12]\nLowest: 12\nHighest: 12","hidden":true}],"require":[],"forbid":[]}]}