sort Summary Sorts the elements of the array. Signatures Click on a signature to select it and view its documentation. public T[] sort() public T[] sort(Comparison(T element1, T element2) element1) Usage public T[] sort() public T[] sort(Comparison(T element1, T element2) element1) Returns The sorted array. The sorted array. Parameters element1 An array element to compare. Description Sorts the elements of the array. For internal types, the sorting is only available for types implementing the IComparable<T> interface and the sorting is performed based on the type's implementation of the compare method of IComparable<T>. Thus, there is a specialization for internal numeric arrays so that they are sorted numerically. For external JavaScript types, the sorting behavior is equivalent to JavaScript's. Please note that this also means that an external array or an array composed of numeric external values will be sorted based on a string sort (the default behavior for JavaScript). This method mutates the array. Differences to JavaScript sort() performs a numeric sort if the underlying Array type is byte, signed byte, short, unsigned short, int, unsigned int, long, unsigned long, char, float, or double. JavaScript's array sort() method will perform a string comparison so [ 1, 2, 10 ] ends up being sorted as [ 1, 10, 2 ]. Sorts the elements of the array with the specified comparator function. This method mutates the array. Examples Sorting for internal arrays 1234import System; int[] arr = [ 10, 1, 3 ];arr.sort(); // [ 1, 3, 10 ] Sorting for internal arrays with external elements 1234import System; Array<external> arr = [ 10, 1, 3 ];arr.sort(); // [ 1, 10, 3 ] Sorting for external arrays 1234import System; var arr = [ 10, 1, 3 ];arr.sort(); // [ 1, 10, 3 ] Sorting Jagged Arrays 1234567891011121314151617import System;import System.Assert; int[][] arr = [ [ 4, 5, 6 ], [ 1, 2, 3 ], [ 7, 8, 9 ] ];// Sort based on first inner array elementarr.sort(Comparison(int[] element1, int[] element2) { if (element1.first() < element2.first()) { return Comparison.LESS_THAN; } else if (element1.first() > element2.first()) { return Comparison.GREATER_THAN; } else { assert(element1.first() == element2.first()); return Comparison.EQUAL; }}); Share HTML | BBCode | Direct Link