List.createSorted method

Creates a live sorted projection over this list. As the list changes, the sorted projection reacts to those changes and may also change.

Syntax

var sortedListProjection = list.createSorted(sorter);

Parameters

  • sorter
    Type: Function

    A function that accepts two arguments. The function is called with elements in the list. It must return one of the following numeric values: negative if the first argument is less than the second, zero if the two arguments are equivalent, positive if the first argument is greater than the second.

    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

Type: SortedListProjection

A sorted projection over the list.

Examples

The following code shows how to use this function.

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

var sorted = "";

for (i = 0; i < sortedList.length; i++) {
    sorted += sortedList.getItem(i).data;
}

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

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

// sorted is "97654321", unsorted is "24697531". 

Requirements

Minimum WinJS version

WinJS 1.0

Namespace

WinJS.Binding

See also

List