sort

Summary

Sorts the elements of the array.

Signatures

Click on a signature to select it and view its documentation.

Usage

public T[] sort()

Returns

The sorted array.

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 ].

Examples

Sorting for internal arrays
1
2
3
4
import System;
 
int[] arr = [ 10, 1, 3 ];
arr.sort(); // [ 1, 3, 10 ]
Sorting for internal arrays with external elements
1
2
3
4
import System;
 
Array<external> arr = [ 10, 1, 3 ];
arr.sort(); // [ 1, 10, 3 ]
Sorting for external arrays
1
2
3
4
import System;
 
var arr = [ 10, 1, 3 ];
arr.sort(); // [ 1, 10, 3 ]

Share

HTML | BBCode | Direct Link