sjkaliski/numbers.js — pull request #37 by btmills (2012-12-13 13:25:03+00:00)
Title: Quantile function, median multidigit bugfix

### Quantile function

`quantile(array, k, q)` computes the kth q-quantile of an unsorted array as defined in the [relevant Wikipedia entry](http://en.wikipedia.org/wiki/Quantile#Quantiles_of_a_population). It does not mutate the array. For example, the lower quartile (1st 4-quantile) of {3, 6, 7, 8, 8, 10, 13, 15, 16, 20} is 7.

### Median multidigit bugfix

While testing the quantile function, I noticed that the array sort wasn't happening correctly in the median function. Previously, `median([0, 2, 15])` returned 15. [According to MDN](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort), sorting an array using `sorted.sort()` sorts alphabetically, so the sorted array became `[0, 15, 2]`. One of the tests for median has been updated to catch this. The solution was to pass a compare function:

sorted.sort(function (a, b) { return a - b; });

This will numerically sort the array to be in the correct order `[0, 2, 15]`. Rather than having nearly-duplicate code, the easiest way to fix this was to make `median(arr)` internally call the logically-equivalent `quantile(arr, 1, 2)`.

### Proposed quantile wrappers

It might be useful to add two additional functions, `quartile()` and `percentile()`, since these are both common statistical measurements.