function sort
sort(
x: number,
mode?: string,
dim?: number,
): number

Sorts an array or matrix in ascending or descending order.

If input is a 1D array, it is sorted normally. If input is a matrix, sorting can be done along rows or columns.

Examples

Sort an array in ascending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([0, 5, -1, 3, -4, 9, 0], 'ascend'), [-4, -1, 0, 0, 3, 5, 9]);

Sort an array in descending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([0, 5, -1, 3, -4, 9, 0], 'descend'), [9, 5, 3, 0, 0, -1, -4]);

Sort rows in descending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([[-1, 3, -1], [4, 5, 9]], 'descend', 1), [
  [4, -1],
  [5, 3],
  [9, -1]
]);

Sort columns in ascending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([[-1, 3, -1], [4, 5, 9]], 'ascend', 0), [
  [-1, -1, 3],
  [4, 5, 9]
]);

Invalid sorting mode

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertThrows(() => sort([1, 2, 3], 'wrong'), Error, 'sorting must be "ascend" or "descend"');

Sort a single number (should return the number itself)

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort(5), 5);

Parameters

x: number

The array or matrix to sort.

optional
mode: string

Sorting order: 'ascend' or 'descend'. Defaults to 'ascend'.

optional
dim: number

Dimension along which to sort (0: rows, 1: columns). Defaults to 1.

Return Type

number

The sorted array or matrix.

Throws

If input is invalid.

sort(
x: array,
mode?: string,
dim?: number,
): array

Sorts an array or matrix in ascending or descending order.

If input is a 1D array, it is sorted normally. If input is a matrix, sorting can be done along rows or columns.

Examples

Sort an array in ascending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([0, 5, -1, 3, -4, 9, 0], 'ascend'), [-4, -1, 0, 0, 3, 5, 9]);

Sort an array in descending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([0, 5, -1, 3, -4, 9, 0], 'descend'), [9, 5, 3, 0, 0, -1, -4]);

Sort rows in descending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([[-1, 3, -1], [4, 5, 9]], 'descend', 1), [
  [4, -1],
  [5, 3],
  [9, -1]
]);

Sort columns in ascending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([[-1, 3, -1], [4, 5, 9]], 'ascend', 0), [
  [-1, -1, 3],
  [4, 5, 9]
]);

Invalid sorting mode

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertThrows(() => sort([1, 2, 3], 'wrong'), Error, 'sorting must be "ascend" or "descend"');

Sort a single number (should return the number itself)

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort(5), 5);

Parameters

The array or matrix to sort.

optional
mode: string

Sorting order: 'ascend' or 'descend'. Defaults to 'ascend'.

optional
dim: number

Dimension along which to sort (0: rows, 1: columns). Defaults to 1.

Return Type

The sorted array or matrix.

Throws

If input is invalid.

sort(
x: matrix,
mode?: string,
dim?: number,
): matrix

Sorts an array or matrix in ascending or descending order.

If input is a 1D array, it is sorted normally. If input is a matrix, sorting can be done along rows or columns.

Examples

Sort an array in ascending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([0, 5, -1, 3, -4, 9, 0], 'ascend'), [-4, -1, 0, 0, 3, 5, 9]);

Sort an array in descending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([0, 5, -1, 3, -4, 9, 0], 'descend'), [9, 5, 3, 0, 0, -1, -4]);

Sort rows in descending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([[-1, 3, -1], [4, 5, 9]], 'descend', 1), [
  [4, -1],
  [5, 3],
  [9, -1]
]);

Sort columns in ascending order

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort([[-1, 3, -1], [4, 5, 9]], 'ascend', 0), [
  [-1, -1, 3],
  [4, 5, 9]
]);

Invalid sorting mode

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertThrows(() => sort([1, 2, 3], 'wrong'), Error, 'sorting must be "ascend" or "descend"');

Sort a single number (should return the number itself)

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(sort(5), 5);

Parameters

The array or matrix to sort.

optional
mode: string

Sorting order: 'ascend' or 'descend'. Defaults to 'ascend'.

optional
dim: number

Dimension along which to sort (0: rows, 1: columns). Defaults to 1.

Return Type

The sorted array or matrix.

Throws

If input is invalid.