# Median

 Jakob Jenkov Last update: 2016-02-05

The median of a data set is found by sorting the values in the data set and taking the value that is exactly in the middle of the data set. I will illustrate the median using the following example data set:

Item Amount Order Id Customer Id
Hard disk99.9579023
Monitor195.9579145
Mouse19.9579223

First we need to sort this data set according to the values of the property we want to find the median of. We will sort the example data set according to th `Amount` property. Here is the sorted data set:

Item Amount Order Id Customer Id
Mouse19.9579223
Hard disk99.9579023
Monitor195.9579145

The median `Amount` value is the value found in the record which is exactly in the middle of the data set. In the above example it would be the second record. The median `Amount` value would thus be 99.95 .

In case a data set has an equal number of records there is no record that is exactly in the middle of the data set. In that case the median is found by taking the average value of the two records that are closest to the middle of the data set. As an example, look at this data set:

Item Amount Order Id Customer Id
Mouse17.9579534
Mouse19.9579223
Hard disk99.9579023
Monitor195.9579145

The data set is already sorted according to the `Amount`. The data set has an equal number of records (4) meaning the median has to be calculated as the average of the `Amount` values of two middle records. The `Amount` values of the two middle records are 19.95 and 99.95. The median is thus calculated as:

```median = ratio(19.95 + 99.95, 2)
```

which is 59.95 .

 Tweet Jakob Jenkov
Featured Videos