 ShowAll Questionssorted byDate Posted Baird_S

# Method for calculating median

I imagine this is pretty basic, but I haven't seen any code posted here to calculate the median of a list of numbers.  Here's mine, which could pretty easily be adapted to most uses:

```List<Integer> testvalues =
new List<Integer>();
Double Middle;

testvalues.sort();
decimal sizeOfList = testvalues.size();
system.debug('size of list is '+ sizeOfList);
Middle = sizeOfList.divide(2,1);
system.debug('middle of list is ' + Middle);
system.debug('minimum value is ' + testvalues);
system.debug('max value is ' + testvalues[testvalues.size()-1]);
//Calculate median
if (middle <> middle.round()) {
system.debug('median is '+ testvalues[middle.intValue()]);
}
else {
decimal lowMedianValue = testvalues[middle.intValue()-1];
decimal highMedianValue = testvalues[middle.intValue()];
decimal Median = (lowMedianValue + highMedianValue)/2;
system.debug('median value is ' + Median);
}```

I hope that makes someone's day easier.

Baird Best Answer chosen by Admin (Salesforce Developers)  Anup Jadhav

Like it, but I don't the round() and divide() method since they introduce imprecision when it's not required.

I'd do something like Wikipedia says we should do to calculate sample median:

```Easy explanation of the sample median

In individual series (if number of observation is very low) first one must arrange all the observations in ascending order. Then count(n) total number of observation in given data.

If n is odd then Median (M) = value of ((n + 1)/2)th item term.

If n is even then Median (M) = value of [((n)/2)th item term + ((n)/2 + 1)th item term ]/2

For an odd number of values

As an example, we will calculate the sample median for the following set of observations: 1, 5, 2, 8, 7.

Start by sorting the values: 1, 2, 5, 7, 8.

In this case, the median is 5 since it is the middle observation in the ordered list.

The median is the ((n + 1)/2)th item, where n is the number of values. For example, for the list {1, 2, 5, 7, 8}, we have n = 5, so the median is the ((5 + 1)/2)th item.

median = (6/2)th item
median = 3rd item
median = 5

For an even number of values

As an example, we will calculate the sample median for the following set of observations: 1, 6, 2, 8, 7, 2.

Start by sorting the values: 1, 2, 2, 6, 7, 8.

In this case, the arithmetic mean of the two middlemost terms is (2 + 6)/2 = 4. Therefore, the median is 4 since it is the arithmetic mean of the middle observations in the ordered list.

We also use this formula MEDIAN = {(n + 1 )/2}th item . n = number of values

As above example 1, 2, 2, 6, 7, 8 n = 6 Median = {(6 + 1)/2}th item = 3.5th item. In this case, the median is average of the 3rd number and the next one (the fourth number). The median is (2 + 6)/2 which is 4.```

Code:

```List<Integer> testvalues = new List<Integer>();

// Insert some values

Integer sizeOfList = testvalues.size();
system.debug('size of list is '+ sizeOfList);
Integer index = sizeOfList - 1;
system.debug('the index is '+index);
Decimal median = 0.0;

// sort the list first
testvalues.sort();

//Calculate median
if (Math.mod(sizeOfList, 2) == 0) {
median = (testValues[(index-1)/2] + testValues[(index/2)+1])/2;
}else{
median = testvalues[(index+1)/2];
}

system.debug('the median is: '+median);``` Anup Jadhav

Like it, but I don't the round() and divide() method since they introduce imprecision when it's not required.

I'd do something like Wikipedia says we should do to calculate sample median:

```Easy explanation of the sample median

In individual series (if number of observation is very low) first one must arrange all the observations in ascending order. Then count(n) total number of observation in given data.

If n is odd then Median (M) = value of ((n + 1)/2)th item term.

If n is even then Median (M) = value of [((n)/2)th item term + ((n)/2 + 1)th item term ]/2

For an odd number of values

As an example, we will calculate the sample median for the following set of observations: 1, 5, 2, 8, 7.

Start by sorting the values: 1, 2, 5, 7, 8.

In this case, the median is 5 since it is the middle observation in the ordered list.

The median is the ((n + 1)/2)th item, where n is the number of values. For example, for the list {1, 2, 5, 7, 8}, we have n = 5, so the median is the ((5 + 1)/2)th item.

median = (6/2)th item
median = 3rd item
median = 5

For an even number of values

As an example, we will calculate the sample median for the following set of observations: 1, 6, 2, 8, 7, 2.

Start by sorting the values: 1, 2, 2, 6, 7, 8.

In this case, the arithmetic mean of the two middlemost terms is (2 + 6)/2 = 4. Therefore, the median is 4 since it is the arithmetic mean of the middle observations in the ordered list.

We also use this formula MEDIAN = {(n + 1 )/2}th item . n = number of values

As above example 1, 2, 2, 6, 7, 8 n = 6 Median = {(6 + 1)/2}th item = 3.5th item. In this case, the median is average of the 3rd number and the next one (the fourth number). The median is (2 + 6)/2 which is 4.```

Code:

```List<Integer> testvalues = new List<Integer>();

// Insert some values

Integer sizeOfList = testvalues.size();
system.debug('size of list is '+ sizeOfList);
Integer index = sizeOfList - 1;
system.debug('the index is '+index);
Decimal median = 0.0;

// sort the list first
testvalues.sort();

//Calculate median
if (Math.mod(sizeOfList, 2) == 0) {
median = (testValues[(index-1)/2] + testValues[(index/2)+1])/2;
}else{
median = testvalues[(index+1)/2];
}

system.debug('the median is: '+median);```

This was selected as the best answer Baird_S

Thanks, Anup.  Good to have some code up for people to use. Ruzan Morrison
Is there a suggestion on how to use Median calculation on a dashboard or in a report? Paul Schembri
Hey all!

I'll apologise in advance for my lack of dev understanding.
Hopefully my question makes sense!

But is some of the code above a potential workaround to get the 'median' in some reporting?
If so, is it possible for the original posters to elaborate a little more on how it can be used?
Im looking to get the Median on how long particular cases have been 'Open' in SalesForce.

Thanks