Skip to content

Commit ab1f0e9

Browse files
authored
feat: Add StaticArray#sort (#2062)
1 parent c23059a commit ab1f0e9

File tree

5 files changed

+3008
-971
lines changed

5 files changed

+3008
-971
lines changed

std/assembly/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,7 @@ declare class StaticArray<T> {
17331733
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
17341734
concat(items: Array<T>): Array<T>;
17351735
slice(from: i32, to?: i32): Array<T>;
1736+
sort(comparator?: (a: T, b: T) => i32): this;
17361737
join(separator?: string): string;
17371738
toString(): string;
17381739
}

std/assembly/staticarray.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path="./rt/index.d.ts" />
22

33
import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
4+
import { COMPARATOR, SORT } from "./util/sort";
45
import { idof } from "./builtins";
56
import { Array } from "./array";
67
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_HOLEYARRAY } from "./util/error";
@@ -229,6 +230,11 @@ export class StaticArray<T> {
229230
return slice;
230231
}
231232

233+
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this {
234+
SORT<T>(changetype<usize>(this), this.length, comparator);
235+
return this;
236+
}
237+
232238
join(separator: string = ","): string {
233239
if (isBoolean<T>()) return joinBooleanArray(changetype<usize>(this), this.length, separator);
234240
if (isInteger<T>()) return joinIntegerArray<T>(changetype<usize>(this), this.length, separator);

0 commit comments

Comments
 (0)