Skip to content

Commit

Permalink
feat: Add StaticArray#sort (#2062)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGraey committed Sep 10, 2021
1 parent c23059a commit ab1f0e9
Show file tree
Hide file tree
Showing 5 changed files with 3,008 additions and 971 deletions.
1 change: 1 addition & 0 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,7 @@ declare class StaticArray<T> {
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
concat(items: Array<T>): Array<T>;
slice(from: i32, to?: i32): Array<T>;
sort(comparator?: (a: T, b: T) => i32): this;
join(separator?: string): string;
toString(): string;
}
Expand Down
6 changes: 6 additions & 0 deletions std/assembly/staticarray.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference path="./rt/index.d.ts" />

import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
import { COMPARATOR, SORT } from "./util/sort";
import { idof } from "./builtins";
import { Array } from "./array";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_HOLEYARRAY } from "./util/error";
Expand Down Expand Up @@ -229,6 +230,11 @@ export class StaticArray<T> {
return slice;
}

sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this {
SORT<T>(changetype<usize>(this), this.length, comparator);
return this;
}

join(separator: string = ","): string {
if (isBoolean<T>()) return joinBooleanArray(changetype<usize>(this), this.length, separator);
if (isInteger<T>()) return joinIntegerArray<T>(changetype<usize>(this), this.length, separator);
Expand Down

0 comments on commit ab1f0e9

Please sign in to comment.