You are currently viewing the GMLscripts.com static mirror. Forum access and script submissions are not available through this mirror.

Invert GMLscripts.com

ds_list_median

Returns the median of the values in the given list.

ds_list_median(list)
Returns the median of the values in the given list.
COPY/// @func   ds_list_median(list)
///
/// @desc   Returns the median of the values in the given list.
///
/// @param  {list}      list        list data structure
///
/// @return {any}       median value
///
/// GMLscripts.com/license

function ds_list_median(list)
{
    var n = ds_list_size(list);
    if (n == 0) return undefined;

    var sort = ds_list_create();

    for (var i=0; i<n; i++) ds_list_add(sort, ds_list_find_value(list, i));
    ds_list_sort(sort, true);
    var middle = ds_list_find_value(sort, n div 2);
    ds_list_destroy(sort);

    return middle;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw