Java Reference
In-Depth Information
// Keep the first 3 elements and delete the rest
var deletedIds2 = ids.splice(3, 2);
print("ids = " + ids);
print("deletedIds2 = " + deletedIds2);
ids = 10,100,200,40,50
deletedIds = 20,30
ids = 10,100,200
deletedIds2 = 40,50
Sorting an Array
The sort(compareFunc) method sorts the array in place and returns the sorted array. The
compareFunc argument, which is a function with two arguments, is optional. If it is not
specified, the array is sorted in alphabetical order, converting the elements to strings, if
necessary, during comparison. Undefined elements are sorted to the end.
If compareFunc is specified, it is passed two elements and its returns value
determines the order of the two elements. Suppose that it is passed x and y. It returns a
negative value if x < y, zero if x = y , and a positive value if x > y . The following code sorts
an integer array in ascending order by not specifying a compareFunc . Later, it sorts the
same array in descending order by specifying a compareFunc :
var ids = [30, 10, 40, 20];
print("ids = " + ids);
// Sort the array
ids.sort();
print("Sorted ids = " + ids);
// A comparison function to sort ids in descending order
var compareDescending = function (x, y) {
if (x > y) {
return -1;
}
else if (x < y) {
return 1;
}
else {
return 0;
}
};
 
Search WWH ::




Custom Search