sorting the resulting arrays by the median