[Question]
  [
Given a list of indices and zero or more lists of integers, output the lists of integers, sorted in ascending order, with the key priority from the first input.
## Example
Let the keys input be `[1, 0, 2]`, and the lists input be `[[5, 3, 4], [6, 2, 1], [5, 2, 1]]`. Those lists need to be sorted by their second element, then first element, then third element, in ascending order:
1. First, we sort by the values at index `1`: `[[6, 2, 1], [5, 2, 1], [5, 3, 4]]`
2. Next, we break any ties from the first sort using the values at index `0`: `[[5, 2, 1], [6, 2, 1], [5, 3, 4]]`
3. Finally, we break any remaining ties with the vlues at index `2` (this doesn't actually change anything, because there are no ties remaining).
## Details
* Sorting is stable: if two elements compare equal with respect to the given sort keys, they must remain in the same relative order in the output. For example, if `A` and `B` are equal under the given sort keys, and the input was `[..., A, ..., B, ...]`, `A` must be placed before `B` in the output.
* A sort key will never reference a non-existant element in one of the input lists.
* No sort key will be repeated. Thus, `[1, 2, 1]` is not a valid list of sort keys.
* Any elements not referenced by the sort keys do not factor into the sorting order. Only the initial relative order and the values of the elements referenced by the sort keys determine the output order.
* You may choose whether the sort keys are zero-indexed or one-indexed.
* There will be no negative values in the sort keys. If you choose to use one-indexing, there will be no zeroes in the sort keys either.
* Integer values will not exceed your language's natively-representable range. If your chosen language is natively capable of arbitrary-precision integers (like Python), then any integer value can be present in the input, subject to memory constraints.
## Reference Implementation (Python 2)
```
#!/usr/bin/env python
keys = input()
lists = input()
print sorted(lists, key=lambda l:[l[x] for x in keys])
```
[Try it online](http://ideone.com/fork/xfxYbP)
## Test Cases
Format: `keys lists -> output`. All sort keys are zero-indexed.
```
[1, 0, 2] [[5, 3, 4], [6, 2, 1], [5, 2, 1]] -> [[5, 2, 1], [6, 2, 1], [5, 3, 4]]
[1, 2] [[5, 3, 4], [6, 2, 1], [5, 2, 1]] -> [[6, 2, 1], [5, 2, 1], [5, 3, 4]]
[0, 1] [[1, 2], [2, 1]] -> [[1, 2], [2, 1]]
[1, 0] [[1, 2], [2, 1]] -> [[2, 1], [1, 2]]
[0] [[4], [10, 11, -88], [-2, 7]] -> [[-2, 7], [4], [10, 11, -88]]
[2] [[-1, -5, 8, -1, -4, -10, -5, 4, 4, 6, -8, 4, 2], [-7, 6, 2, -8, -7, 7, -3, 3, 0, -6, 1], [-9, 8, -5, -1, -7, -8, -5, -6, 5, -6, 6]] -> [[-9, 8, -5, -1, -7, -8, -5, -6, 5, -6, 6], [-7, 6, 2, -8, -7, 7, -3, 3, 0, -6, 1], [-1, -5, 8, -1, -4, -10, -5, 4, 4, 6, -8, 4, 2]]
[2, 1] [[9, 2, -2, -10, -6], [3, -4, -2]] -> [[3, -4, -2], [9, 2, -2, -10, -6]]
[2, 4, 8] [[5, -3, 4, -6, -1, -2, -2, -4, 5], [-2, -3, 6, -4, -1, -4, -4, -5, 8, 9, 9, -3, 3, -9, -3], [2, 0, 10, -10, -1, 2, -1, 5, -1, 10, -5], [-7, -8, -6, 7, 3, 8, 6, -7, -2, 0, -6, -4, 4, -3, 2, -3]] -> [[-7, -8, -6, 7, 3, 8, 6, -7, -2, 0, -6, -4, 4, -3, 2, -3], [5, -3, 4, -6, -1, -2, -2, -4, 5], [-2, -3, 6, -4, -1, -4, -4, -5, 8, 9, 9, -3, 3, -9, -3], [2, 0, 10, -10, -1, 2, -1, 5, -1, 10, -5]]
[1, 2, 3, 4, 5] [[-7, 3, -8, 3, 5, -1, 6, -6, 9, 8], [-9, -1, -7, -9, -10, -2, -8, -10, -10, -3], [5, 3, -6, -5, -4, -4, -8, 2], [9, -4, 1, -1, -3, -2], [-6, -10, 4, -10, 6, 6, -1, 3, 0, 0], [1, -2, -7, -6, -7, -7, -1, 0, -4, 3, 3], [7, -1, -7, 2, -2, 9, 7, 5, -6, -8], [1, -5, -3, -10, -7, 9, -8, -5, -1], [-9, 4, -1, -1, 2, 4]] -> [[-6, -10, 4, -10, 6, 6, -1, 3, 0, 0], [1, -5, -3, -10, -7, 9, -8, -5, -1], [9, -4, 1, -1, -3, -2], [1, -2, -7, -6, -7, -7, -1, 0, -4, 3, 3], [-9, -1, -7, -9, -10, -2, -8, -10, -10, -3], [7, -1, -7, 2, -2, 9, 7, 5, -6, -8], [-7, 3, -8, 3, 5, -1, 6, -6, 9, 8], [5, 3, -6, -5, -4, -4, -8, 2], [-9, 4, -1, -1, 2, 4]]
[8, 7, 3, 2, 4, 9, 1] [[8, -5, 1, -6, -1, -4, 6, 10, 10, 6, 9, 5], [4, -8, 6, -10, -2, -3, 2, -6, 9, 5, 4, 10, 2, 3], [10, -1, 3, 0, -4, 1, -5, -4, -1, -7, 9, -9, -1, -5, 7, 8, 9, 6, -3], [0, -9, -7, -2, 2, -5, 7, 4, 6, -4, 1, 8, -7, 10], [5, 6, -9, 0, -1, 5, 4, 7, 5, 10, 2, 5, 7, -9]] -> [[10, -1, 3, 0, -4, 1, -5, -4, -1, -7, 9, -9, -1, -5, 7, 8, 9, 6, -3], [5, 6, -9, 0, -1, 5, 4, 7, 5, 10, 2, 5, 7, -9], [0, -9, -7, -2, 2, -5, 7, 4, 6, -4, 1, 8, -7, 10], [4, -8, 6, -10, -2, -3, 2, -6, 9, 5, 4, 10, 2, 3], [8, -5, 1, -6, -1, -4, 6, 10, 10, 6, 9, 5]]
```
  
[Answer]
# [Jelly](http://github.com/DennisMitchell/jelly), 4 bytes
```
⁴ị$Þ
```
[Try it online!](http://jelly.tryitonline.net/#code=4oG04buLJMOe&input=&args=W1s4LCAtNSwgMSwgLTYsIC0xLCAtNCwgNiwgMTAsIDEwLCA2LCA5LCA1XSwgWzQsIC04LCA2LCAtMTAsIC0yLCAtMywgMiwgLTYsIDksIDUsIDQsIDEwLCAyLCAzXSwgWzEwLCAtMSwgMywgMCwgLTQsIDEsIC01LCAtNCwgLTEsIC03LCA5LCAtOSwgLTEsIC01LCA3LCA4LCA5LCA2LCAtM10sIFswLCAtOSwgLTcsIC0yLCAyLCAtNSwgNywgNCwgNiwgLTQsIDEsIDgsIC03LCAxMF0sIFs1LCA2LCAtOSwgMCwgLTEsIDUsIDQsIDcsIDUsIDEwLCAyLCA1LCA3LCAtOV1d+WzksIDgsIDQsIDMsIDUsIDEwLCAyXQ)
[Answer]
## [CJam](http://sourceforge.net/projects/cjam/), 13 bytes
```
{W%{{X=}$}fX}
```
An unnamed block which expects the list of lists and the list of priorities on top of the stack and replaces them with the sorted list of lists.
[Try it online!](http://cjam.tryitonline.net/#code=cU4ve35cCgp7VyV7e1g9fSR9Zlh9Cgp-cH0v&input=WzEgMCAyXSBbWzUgMyA0XSBbNiAyIDFdIFs1IDIgMV1dClsxIDJdIFtbNSAzIDRdIFs2IDIgMV0gWzUgMiAxXV0KWzBdIFtbNF0gWzEwIDExIC04OF0gWy0yIDddXQpbMCAxXSBbWzEgMl0gWzIgMV1dClsxIDBdIFtbMSAyXSBbMiAxXV0gClsyXSBbWy0xIC01IDggLTEgLTQgLTEwIC01IDQgNCA2IC04IDQgMl0gWy03IDYgMiAtOCAtNyA3IC0zIDMgMCAtNiAxXSBbLTkgOCAtNSAtMSAtNyAtOCAtNSAtNiA1IC02IDZdXQpbMiAxXSBbWzkgMiAtMiAtMTAgLTZdIFszIC00IC0yXV0KWzIgNCA4XSBbWzUgLTMgNCAtNiAtMSAtMiAtMiAtNCA1XSBbLTIgLTMgNiAtNCAtMSAtNCAtNCAtNSA4IDkgOSAtMyAzIC05IC0zXSBbMiAwIDEwIC0xMCAtMSAyIC0xIDUgLTEgMTAgLTVdIFstNyAtOCAtNiA3IDMgOCA2IC03IC0yIDAgLTYgLTQgNCAtMyAyIC0zXV0KWzEgMiAzIDQgNV0gW1stNyAzIC04IDMgNSAtMSA2IC02IDkgOF0gWy05IC0xIC03IC05IC0xMCAtMiAtOCAtMTAgLTEwIC0zXSBbNSAzIC02IC01IC00IC00IC04IDJdIFs5IC00IDEgLTEgLTMgLTJdIFstNiAtMTAgNCAtMTAgNiA2IC0xIDMgMCAwXSBbMSAtMiAtNyAtNiAtNyAtNyAtMSAwIC00IDMgM10gWzcgLTEgLTcgMiAtMiA5IDcgNSAtNiAtOF0gWzEgLTUgLTMgLTEwIC03IDkgLTggLTUgLTFdIFstOSA0IC0xIC0xIDIgNF1dCls4IDcgMyAyIDQgOSAxXSBbWzggLTUgMSAtNiAtMSAtNCA2IDEwIDEwIDYgOSA1XSBbNCAtOCA2IC0xMCAtMiAtMyAyIC02IDkgNSA0IDEwIDIgM10gWzEwIC0xIDMgMCAtNCAxIC01IC00IC0xIC03IDkgLTkgLTEgLTUgNyA4IDkgNiAtM10gWzAgLTkgLTcgLTIgMiAtNSA3IDQgNiAtNCAxIDggLTcgMTBdIFs1IDYgLTkgMCAtMSA1IDQgNyA1IDEwIDIgNSA3IC05XV0) (As a test suite.)
### Explanation
Sorting with tie breakers can be implemented by repeatedly sorting the entire list stably going from the lowest-priority key to the highest-priority key.
```
W%      e# Reverse priority list.
{       e# For each priority X...
  {     e#   Sort the lists by the result of this block...
    X=  e#     Extract the Xth element from the current list.
  }$
}fX
```
[Answer]
# Ruby, 35 bytes
```
->k,a{a.sort_by{|e|e.values_at *k}}
```
See it on eval.in: <https://eval.in/652574>
[Answer]
## Haskell, 38 bytes
```
import Data.List
sortOn.flip(map.(!!))
```
Usage example: `( sortOn.flip(map.(!!)) ) [2,1] [[9,2,-2,-10,-6], [3,-4,-2]]` -> `[[3,-4,-2],[9,2,-2,-10,-6]]`.
Non-pointfree: `f k v=sortOn(\x->map(\y->x!!y)k)v`.
[Answer]
## Mathematica, ~~22~~ 19 bytes
```
SortBy[Extract/@#]&
```
Uses 1-based indices. This unnamed function is [curried](https://codegolf.meta.stackexchange.com/a/8427/8478), so the calling convention is:
```
SortBy[Extract/@#]&[{2, 1, 3}][{{5, 3, 4}, {6, 2, 1}, {5, 2, 1}}]
```
Mathematica's `SortBy` can take a list of functions in which case the individual functions are used as successive tie-breakers, so that's just what we want. All we need to do is create a list of functions which return the corresponding list element. This can be done with `Extract`. `Extract` is normally a binary function `Extract[list, index]` which returns a list element. However if used unarily, then `Extract[index]` returns a function which retrieves the element at `index` from a list passed to it. In other words, the `index` parameter of `Extract` can be curried. We make use of this by mapping `Extract` over the list of indices we're given, which creates the list of functions we need.
[Answer]
## Python, 50 bytes
```
lambda l,k:sorted(l,key=lambda x:[x[y]for y in k])
```
This is a trivially-golfed version of the reference implementation. `l` is the lists parameter, and `k` is the sort keys parameter. `l` can be any iterable, so long as its elements are subscriptable by integers (like lists, tuples, or int-keyed dicts). `k` can be any iterable.
[Answer]
# [Brachylog](http://github.com/JCumin/Brachylog), 29 bytes
```
tT,?hg:Tz:{:2f}o:ta
heI,?t:Im
```
[Try it online!](http://brachylog.tryitonline.net/#code=dFQsP2hnOlR6Ons6MmZ9bzp0YQpoZUksP3Q6SW0&input=W1s4Ojc6MzoyOjQ6OToxXTpbWzg6XzU6MTpfNjpfMTpfNDo2OjEwOjEwOjY6OTo1XTpbNDpfODo2Ol8xMDpfMjpfMzoyOl82Ojk6NTo0OjEwOjI6M106WzEwOl8xOjM6MDpfNDoxOl81Ol80Ol8xOl83Ojk6Xzk6XzE6XzU6Nzo4Ojk6NjpfM106WzA6Xzk6Xzc6XzI6MjpfNTo3OjQ6NjpfNDoxOjg6Xzc6MTBdOls1OjY6Xzk6MDpfMTo1OjQ6Nzo1OjEwOjI6NTo3Ol85XV1d&args=Wg)
### Explanation
We use the fact that `o - Order` can be used with an additionnal predicate as input: we order the lists by using for each `[Keys, a list]` the list of the elements of `a list` which are at index `a key` in the order that they appear in `Keys`.
```
                          Input = [Keys, List of lists]
tT,                       Call the Keys T
   ?hg:T                  Create the list [[T], List of lists]
        z                 Zip [T] with the list of lists
         :{   }o          Order by the output of this predicate
                :ta       Keep only the last element of each sublist in the Output
 
           :2f            Find all outputs of the predicate below
heI,                      Take a key I
    ?t:Im                 Output is the Ith element of the sublist
```
[Answer]
## CJam (12 bytes)
```
{{1$\f=}$\;}
```
[Online demo](http://cjam.aditsu.net/#code=qN%2F%7B~%0A%0A%7B%7B1%24%5Cf%3D%7D%24%5C%3B%7D%0A%0A~p%7D%2F&input=%5B1%200%202%5D%20%5B%5B5%203%204%5D%20%5B6%202%201%5D%20%5B5%202%201%5D%5D%0A%5B1%202%5D%20%5B%5B5%203%204%5D%20%5B6%202%201%5D%20%5B5%202%201%5D%5D%0A%5B0%201%5D%20%5B%5B1%202%5D%20%5B2%201%5D%5D%0A%5B1%200%5D%20%5B%5B1%202%5D%20%5B2%201%5D%5D%0A%5B0%5D%20%5B%5B4%5D%20%5B10%2011%20-88%5D%20%5B-2%207%5D%5D%0A%5B2%5D%20%5B%5B-1%20-5%208%20-1%20-4%20-10%20-5%204%204%206%20-8%204%202%5D%20%5B-7%206%202%20-8%20-7%207%20-3%203%200%20-6%201%5D%20%5B-9%208%20-5%20-1%20-7%20-8%20-5%20-6%205%20-6%206%5D%5D%0A%5B2%201%5D%20%5B%5B9%202%20-2%20-10%20-6%5D%20%5B3%20-4%20-2%5D%5D%0A%5B2%204%208%5D%20%5B%5B5%20-3%204%20-6%20-1%20-2%20-2%20-4%205%5D%20%5B-2%20-3%206%20-4%20-1%20-4%20-4%20-5%208%209%209%20-3%203%20-9%20-3%5D%20%5B2%200%2010%20-10%20-1%202%20-1%205%20-1%2010%20-5%5D%20%5B-7%20-8%20-6%207%203%208%206%20-7%20-2%200%20-6%20-4%204%20-3%202%20-3%5D%5D%0A%5B1%202%203%204%205%5D%20%5B%5B-7%203%20-8%203%205%20-1%206%20-6%209%208%5D%20%5B-9%20-1%20-7%20-9%20-10%20-2%20-8%20-10%20-10%20-3%5D%20%5B5%203%20-6%20-5%20-4%20-4%20-8%202%5D%20%5B9%20-4%201%20-1%20-3%20-2%5D%20%5B-6%20-10%204%20-10%206%206%20-1%203%200%200%5D%20%5B1%20-2%20-7%20-6%20-7%20-7%20-1%200%20-4%203%203%5D%20%5B7%20-1%20-7%202%20-2%209%207%205%20-6%20-8%5D%20%5B1%20-5%20-3%20-10%20-7%209%20-8%20-5%20-1%5D%20%5B-9%204%20-1%20-1%202%204%5D%5D%0A%5B8%207%203%202%204%209%201%5D%20%5B%5B8%20-5%201%20-6%20-1%20-4%206%2010%2010%206%209%205%5D%20%5B4%20-8%206%20-10%20-2%20-3%202%20-6%209%205%204%2010%202%203%5D%20%5B10%20-1%203%200%20-4%201%20-5%20-4%20-1%20-7%209%20-9%20-1%20-5%207%208%209%206%20-3%5D%20%5B0%20-9%20-7%20-2%202%20-5%207%204%206%20-4%201%208%20-7%2010%5D%20%5B5%206%20-9%200%20-1%205%204%207%205%2010%202%205%207%20-9%5D%5D). This is an anonymous block (function) which takes the arguments in the order given for the test cases and leaves the sorted value on the stack. It relies on the built-in sort `$` being stable, but the official interpreter guarantees that.
### Dissection
```
{          e# Define a block. Stack: orders values-to-sort
  {        e#   Sort by...
    1$\f=  e#     Copy orders from the stack, and map array lookup
  }$
  \;       e#   Pop the orders to leave just sorted-values
}
```
[Answer]
# J, 6 bytes
```
]/:{&>
```
Keys are zero-indexed. The LHS is the list of keys and the RHS is an array of values. Since J doesn't support ragged arrays, each array must be boxed.
## Usage
```
   f =: ]/:{&>
   < 1 0 2
┌─────┐
│1 0 2│
└─────┘
   5 3 4 ; 6 2 1 ; 5 2 1
┌─────┬─────┬─────┐
│5 3 4│6 2 1│5 2 1│
└─────┴─────┴─────┘
   (< 1 0 2) f  5 3 4 ; 6 2 1 ; 5 2 1
┌─────┬─────┬─────┐
│5 2 1│6 2 1│5 3 4│
└─────┴─────┴─────┘
   (< 1 2) f  5 3 4 ; 6 2 1 ; 5 2 1
┌─────┬─────┬─────┐
│6 2 1│5 2 1│5 3 4│
└─────┴─────┴─────┘
   (< 0) f 4 ; 10 11 _88 ; _2 7
┌────┬─┬─────────┐
│_2 7│4│10 11 _88│
└────┴─┴─────────┘
```
## Explanation
```
]/:{&>  Input: keys (LHS), values (RHS)
   {&>  Select from values at each index in keys
]       Get the values
 /:     Sort up the values using the ones selected with the keys
```
[Answer]
## JavaScript (ES6), 55 bytes
```
(k,l)=>k.reduceRight((l,i)=>l.sort((a,b)=>a[i]-b[i]),l)
```
The ECMAscript standard doesn't guarantee that the underlying sort is stable, so the following 68-byte code doesn't make that assumption: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C+3%2C+0%2C+-4%2C+1%2C+-5%2C+-4%2C+-1%2C+-7%2C+9%2C+-9%2C+-1%2C+-5%2C+7%2C+8%2C+9%2C+6%2C+-3%5D%2C+%5B0%2C+-9%2C+-7%2C+-2%2C+2%2C+-5%2C+7%2C+4%2C+6%2C+-4%2C+1%2C+8%2C+-7%2C+10%5D%2C+%5B5%2C+6%2C+-9%2C+0%2C+-1%2C+5%2C+4%2C+7%2C+5%2C+10%2C+2%2C+5%2C+7%2C+-9%5D%5D&debug=0)
Thanks to @Maltysen for one byte.
# Explanation:
```
@LDFQ   Q (=input) added implicitly. 
  D     sort a list of lists by
@L         the sublists generated by some indices
   FQ   executes ^ with the the input as parameter
```
I was really surprised that this worked. It's a really strange syntax.
[Answer]
# JavaScript (ES6) 46
```
k=>l=>l.sort((a,b)=>k.some(i=>v=a[i]-b[i])&&v)
```
At each comparison during the sort, scan the key indices to find the right order
**Test**
```
f=k=>l=>l.sort((a,b)=>k.some(i=>v=a[i]-b[i])&&v)
;`[1, 0, 2] [[5, 3, 4], [6, 2, 1], [5, 2, 1]] -> [[5, 2, 1], [6, 2, 1], [5, 3, 4]]
[1, 2] [[5, 3, 4], [6, 2, 1], [5, 2, 1]] -> [[6, 2, 1], [5, 2, 1], [5, 3, 4]]
[0, 1] [[1, 2], [2, 1]] -> [[1, 2], [2, 1]]
[1, 0] [[1, 2], [2, 1]] -> [[2, 1], [1, 2]]
[0] [[4], [10, 11, -88], [-2, 7]] -> [[-2, 7], [4], [10, 11, -88]]
[2] [[-1, -5, 8, -1, -4, -10, -5, 4, 4, 6, -8, 4, 2], [-7, 6, 2, -8, -7, 7, -3, 3, 0, -6, 1], [-9, 8, -5, -1, -7, -8, -5, -6, 5, -6, 6]] -> [[-9, 8, -5, -1, -7, -8, -5, -6, 5, -6, 6], [-7, 6, 2, -8, -7, 7, -3, 3, 0, -6, 1], [-1, -5, 8, -1, -4, -10, -5, 4, 4, 6, -8, 4, 2]]
[2, 1] [[9, 2, -2, -10, -6], [3, -4, -2]] -> [[3, -4, -2], [9, 2, -2, -10, -6]]
[2, 4, 8] [[5, -3, 4, -6, -1, -2, -2, -4, 5], [-2, -3, 6, -4, -1, -4, -4, -5, 8, 9, 9, -3, 3, -9, -3], [2, 0, 10, -10, -1, 2, -1, 5, -1, 10, -5], [-7, -8, -6, 7, 3, 8, 6, -7, -2, 0, -6, -4, 4, -3, 2, -3]] -> [[-7, -8, -6, 7, 3, 8, 6, -7, -2, 0, -6, -4, 4, -3, 2, -3], [5, -3, 4, -6, -1, -2, -2, -4, 5], [-2, -3, 6, -4, -1, -4, -4, -5, 8, 9, 9, -3, 3, -9, -3], [2, 0, 10, -10, -1, 2, -1, 5, -1, 10, -5]]
[1, 2, 3, 4, 5] [[-7, 3, -8, 3, 5, -1, 6, -6, 9, 8], [-9, -1, -7, -9, -10, -2, -8, -10, -10, -3], [5, 3, -6, -5, -4, -4, -8, 2], [9, -4, 1, -1, -3, -2], [-6, -10, 4, -10, 6, 6, -1, 3, 0, 0], [1, -2, -7, -6, -7, -7, -1, 0, -4, 3, 3], [7, -1, -7, 2, -2, 9, 7, 5, -6, -8], [1, -5, -3, -10, -7, 9, -8, -5, -1], [-9, 4, -1, -1, 2, 4]] -> [[-6, -10, 4, -10, 6, 6, -1, 3, 0, 0], [1, -5, -3, -10, -7, 9, -8, -5, -1], [9, -4, 1, -1, -3, -2], [1, -2, -7, -6, -7, -7, -1, 0, -4, 3, 3], [-9, -1, -7, -9, -10, -2, -8, -10, -10, -3], [7, -1, -7, 2, -2, 9, 7, 5, -6, -8], [-7, 3, -8, 3, 5, -1, 6, -6, 9, 8], [5, 3, -6, -5, -4, -4, -8, 2], [-9, 4, -1, -1, 2, 4]]
[8, 7, 3, 2, 4, 9, 1] [[8, -5, 1, -6, -1, -4, 6, 10, 10, 6, 9, 5], [4, -8, 6, -10, -2, -3, 2, -6, 9, 5, 4, 10, 2, 3], [10, -1, 3, 0, -4, 1, -5, -4, -1, -7, 9, -9, -1, -5, 7, 8, 9, 6, -3], [0, -9, -7, -2, 2, -5, 7, 4, 6, -4, 1, 8, -7, 10], [5, 6, -9, 0, -1, 5, 4, 7, 5, 10, 2, 5, 7, -9]] -> [[10, -1, 3, 0, -4, 1, -5, -4, -1, -7, 9, -9, -1, -5, 7, 8, 9, 6, -3], [5, 6, -9, 0, -1, 5, 4, 7, 5, 10, 2, 5, 7, -9], [0, -9, -7, -2, 2, -5, 7, 4, 6, -4, 1, 8, -7, 10], [4, -8, 6, -10, -2, -3, 2, -6, 9, 5, 4, 10, 2, 3], [8, -5, 1, -6, -1, -4, 6, 10, 10, 6, 9, 5]]`
.split('\n').map(row=>{
  var [keys,list,expected]=row.split(/] -?>? ?\[/)
  keys=eval(keys+']');
  list=eval('['+list+']');
  expected=eval('['+expected);
  var result=f(keys)(list);
  var ok=result.join`|`==expected.join`|`;
  console.log(ok?'OK':'KO',keys+' '+list.join`|`+' -> ' +expected.join`|`,ok?'':result.join`|`)
})
```
[Answer]
# [Perl 6](http://perl6.org) ~~23 20~~ 19 bytes
```
->\a,\b{b.sort:{.[|a]}}
{$^a;$^b.sort:{.[|$a]}}
{$^b.sort: *.[|$^a]}
```
```
{$^b.sort: *[|$^a]}
```
[Answer]
## PHP, ~~212~~ 170 bytes
```
function m(&$i,$k){foreach($i as$a=>$y)for($b=-1;++$b<$a;){for($p=0;$p<count($k)&!$r=$i[$b][$x=$k[$p++]]-$i[$b+1][$x];);if($r>0){$s=$i[$b+1];$i[$b+1]=$i[$b];$i[$b]=$s;}}}
```
PHP has [no stable sort built in anymore](https://bugs.php.net/bug.php?id=53341); picking an older version there is no way to do a recursive callback with the required specification. But nevermind: using the [recursive callback iterator](http://php.net/manual/class.recursivecallbackfilteriterator) would cost tons; so I didn´t even check if that could do it.
The inner loop could be replaced with another `foreach`; that would save some bytes on the swapping. But without a check for `$b<$a` (or `$b<count($i)`), that will result in an infinite loop. And with that check, The `foreach` costs as much as the swapping wins.
I first did the comparison recursively; but iteration saves tons of bytes:
**breakdown**
```
// bubble sort
function m(&$i,$k)
{
    foreach($i as$a=>$y)
        for($b=-1;++$b<$a;)
        {
            // comparison:
            for($p=0;$p<count($k)                       // loop through keys
                &
                !$r=$i[$b][$x=$k[$p++]]-$i[$b+1][$x]    // while element equals its successor
            ;);
            // if element is larger than its successor, swap them
            if($r>0)
            {
                $s=$i[$b+1];$i[$b+1]=$i[$b];$i[$b]=$s;
            }
        }
}
```
[Answer]
## R 40 bytes
```
for(i in rev(il)){dd=dd[order(dd[,i]),]}
```
Explanation: 
List of lists is best represented as a data.frame in R: 
```
ll2 = list(c(5,3,4), c(5,3,7), c(6,2,1), c(6,1,3), c(5,2,1))
dd = data.frame(do.call(rbind, ll2))
dd
      X1 X2 X3
    1  5  3  4
    2  5  3  7
    3  6  2  1
    4  6  1  3
    5  5  2  1
```
If index list is il (indexing is from 1): 
```
il = list(1, 2, 3)
```
Sorting can be done with following code: 
```
for(i in rev(il)){dd = dd[order(dd[,i]),]}
```
Output: 
```
dd
  X1 X2 X3
5  5  2  1
1  5  3  4
2  5  3  7
4  6  1  3
3  6  2  1
```
[Answer]
## Racket 218 bytes
```
(λ(il ll)(define qsl(λ(l i)(if(null? l)l(let*((h(first l))(t(rest l))(g(λ(ff)(filter(λ(x)(ff(list-ref x i)
(list-ref h i)))t))))(append(qsl(g <)i)(list h)(qsl(g >=)i))))))(for((i(reverse il)))(set! ll(qsl ll i)))ll)
```
Ungolfed (il=index list; ll=list of lists; qsl= quicksort for list of lists; h=head (first element); t=tail (rest or remaining elements); g=modifiable filter fn): 
```
(define qsl
  (λ(l i)
    (if (null? l)
        l
        (let* ((h (first l))
               (t (rest  l))
               (g (λ(ff) (filter (λ(x) (ff (list-ref x i) (list-ref h i))) t))))
          (append (qsl (g <) i)
                  (list h)
                  (qsl (g >=) i)
                  )))))
(define f
  (λ(il ll)
    (for ((i (reverse il)))
      (set! ll (qsl ll i)))
    ll))
```
Testing: 
```
(f (list 0 1 2) (list (list 5 3 4) (list 5 3 7) (list 6 2 1) (list 6 1 3) (list 5 2 1)))
(f [list 1 2] [list [list 5 3 4] [list 6 2 1] [list 5 2 3]])
```
Output: 
```
'((5 2 1) (5 3 4) (5 3 7) (6 1 3) (6 2 1))
'((6 2 1) (5 2 3) (5 3 4))
```
[Answer]
# PHP, 139 Bytes
use the new [spaceship operator](http://php.net/manual/en/language.operators.comparison.php) and [usort](http://php.net/manual/en/function.usort.php) 
```
<?$a=$_GET[a];function c($x,$y,$i=0){$k=$_GET[k];return$x[$k[$i]]-$y[$k[$i]]?:($k[++$i]?c($x,$y,$i):0);}usort($a,c);echo json_encode($a);
```
Instead of `$x[$k[$i]]<=>$y[$k[$i]]` you can use `$x[$k[$i]]-$y[$k[$i]]` under a PHP Version greater 7 - 2 Bytes
version with create an own index 197 Bytes like in a real library
```
<?$m=min(array_map(min,$a=$_GET[a]));foreach($a as$p=>$v){$k="";foreach($s=$_GET[s]as$f){$k.=str_pad($v[$f]-$m,5,0,0);}$k.=str_pad($p,5,0,0);$r[$k]=$v;}ksort($r);echo json_encode(array_values($r));
```
[Answer]
# [K (ngn/k)](https://codeberg.org/ngn/k), 10 bytes
```
{y@<y[;x]}
```
[Try it online!](https://ngn.bitbucket.io/k#eJy1VMtuAjEMvPMVPoLUSJvHJpumh/4H4so3FFXtt9czTpBKyy49VAvLrj0e22OH8/P75fXlcmxvp4/dbj9LEN8y77NESYfP89HLJKHJnoYm5hVCD6fdfqBvYlYi4MSj4iY4aBhOQPW9J75xuiClPaXmNc6LWxZyKEhtcjU2AY74Kou4WZzai7r4nIW33GBDbbAXUX/Ueif4tCiEzIjW36T3Ca9Jrwx8sgrR5CZwI89aieghklZZKiiCZcjMTvG+mZt0OLs3vqwpoybJTBAss0OFWogGRoxGHxPtnmQBgJlCqidbZ7wn67bqxT5cJQNoUQG/YPXCniiHFZtk4U78a6omj3ZNhTJpbGxZWBNGoyvFqSqUaQpK6GPCIJTHs8rIyVgjhRkKPzgzCtKqOeE+3GpstgmjBUWUDqAaVV85fbewmQh0tBYz7BU64mgBMw+lFi5aFVMPuoyDiEMIgfd3ybYr/D3dDyHW9VzR6L4CK3O46Rb/LNPIaIXNY5sYWPuhLlyq3BvLcEy2RYmZlSUIYK42eGpfoWDByZbUC4+z196oR75Kx/2CtCAkmSZi1X7sPTg815g4TGrhwuKYVJ7r1YDHcv5djke63ZDscPoCdPEd+Q==)
[Answer]
**Clojure, 29 bytes**
```
#(sort-by(fn[c](mapv c %))%2)
```
Nicely `sort-by` is stable and knows how to sort vectors, and vectors can operate as functions. `([4 6 9 7] 2)` is `9` (0-based indexing).
]