The trigger should look at all contacts on an account and find the median NPS Score and update the field on the account called Median NPS score.
trigger NPSScore on Contact (before insert) {
for (Contact contact : trigger.new){
Set<Id> ContactId = new Set<Id>();
List<Integer> contactvalues = new List<Integer>();
for(Contact c:[Select Id, NPS_Rating__c from Contact where NPS_Rating__c != null ])
Integer sizeOfList = contactvalues.size();
Integer index = sizeOfList - 1;
Decimal median = 0.0;
// sort the list first
contactvalues.sort();
//Calculate median
if (Math.mod(sizeOfList, 2) == 0) {
median = (contactValues[(index-1)/2] + contactValues[(index/2)+1])/2;
}else{
median = contactvalues[(index+1)/2];
}
system.debug('the median is: '+median);
account.Median_NPS_Score__c = median;
}
}

