// Numbas version: exam_results_page_options {"name": "Sort a list of lists by a particular key", "extensions": [], "custom_part_types": [], "resources": [], "navigation": {"allowregen": true, "showfrontpage": false, "preventleave": false, "typeendtoleave": false}, "question_groups": [{"pickingStrategy": "all-ordered", "questions": [{"name": "Sort a list of lists by a particular key", "tags": [], "metadata": {"description": "

Obosolete: there's now a built-in function sort_by.

\n

Defines a custom function sort_by to sort a list of lists by the elements at the given index.

\n

You could do a similar thing to sort lists of dictionaries.

", "licence": "Creative Commons Attribution 4.0 International"}, "statement": "

{table(zip(names,order),['Name','Position'])}

\n

The order is {join(sorted,', ')}

", "advice": "", "rulesets": {}, "extensions": [], "variables": {"names": {"name": "names", "group": "Ungrouped variables", "definition": "[\"A\",\"B\",\"C\",\"D\",\"E\",\"F\"]", "description": "

The list that we want to sort.

", "templateType": "anything"}, "order": {"name": "order", "group": "Ungrouped variables", "definition": "shuffle(1..len(names))", "description": "

The values that will be used to order the corresponding elements of names.

", "templateType": "anything"}, "sorted": {"name": "sorted", "group": "Ungrouped variables", "definition": "map(\n x[0],\n x,\n sort_by(1,zip(names,order))\n)", "description": "
    \n
  1. Zip names and order together, to form a list of items of the form [name,order]
  2. \n
  3. Sort the zipped list by item 1, using the custom function sort_by.
  4. \n
  5. Get just the name for each item in the sorted list
  6. \n
", "templateType": "anything"}}, "variablesTest": {"condition": "", "maxRuns": 100}, "ungrouped_variables": ["names", "order", "sorted"], "variable_groups": [], "functions": {"sort_by": {"parameters": [["index", "number"], ["list", "list"]], "type": "list", "language": "javascript", "definition": "return list.slice().sort(function(a,b) {\n a = a[index];\n b = b[index];\n return a > b ? 1 : a < b ? -1 : 0;\n})"}}, "preamble": {"js": "", "css": ""}, "parts": [], "partsMode": "all", "maxMarks": 0, "objectives": [], "penalties": [], "objectiveVisibility": "always", "penaltyVisibility": "always", "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}]}]}], "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}]}