diff --git a/src/js.rs b/src/js.rs index 0a339160..bd6eb5f5 100644 --- a/src/js.rs +++ b/src/js.rs @@ -151,6 +151,17 @@ extern { #[wasm_bindgen(method)] pub fn shift(this: &Array) -> JsValue; + /// The sort() method sorts the elements of an array in place and returns + /// the array. The sort is not necessarily stable. The default sort + /// order is according to string Unicode code points. + /// + /// The time and space complexity of the sort cannot be guaranteed as it + /// is implementation dependent. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort + #[wasm_bindgen(method)] + pub fn sort(this: &Array) -> Array; + /// The toString() method returns a string representing the specified array and its elements. /// /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index ae6f893e..2ce9ce39 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -40,6 +40,36 @@ fn index_of() { .test() } +#[test] +fn sort() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js; + + #[wasm_bindgen] + pub fn sort_array(this: &js::Array) -> js::Array { + this.sort() + } + + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let numbers = [3, 1, 6, 2]; + let sorted = wasm.sort_array(numbers); + + assert.deepStrictEqual(sorted, [1, 2, 3, 6]) + } + "#) + .test() +} + #[test] fn last_index_of() { project()