Compartir a través de


List.sort method

Returns a list with the elements sorted. This method sorts the elements of a list object in place. It does not create a new list object during execution.

Syntax

list.sort(sortFunction);

Parameters

  • sortFunction
    Type: Function

    The function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.

    This function must always return the same results, given the same inputs. The results should not depend on values that are subject to change. You must call notifyMutated each time an item changes. Do not batch change notifications.

Return value

This method does not return a value.

Examples

The following example shows how to use this function. The compare function sorts the members of the array

var numArr = new Array(2, 4, 6, 9, 7, 5, 3, 1);
var list = new WinJS.Binding.List(numArr);
list.sort(descendingCompare);

var str = "";

for (i = 0; i < list.length; i++) {
    str += list.getItem(i).data;
}

function descendingCompare(first, second) {
    if (first == second)
        return 0;
    else if (first < second)
         return 1;
    else
         return -1;
}

// str is "97654321". 

Requirements

Minimum WinJS version

WinJS 1.0

Namespace

WinJS.Binding

See also

List