Skip to content

Commit

Permalink
feat(math): add math.sort() function to sort numbers in ascending o…
Browse files Browse the repository at this point in the history
…r descending order.
  • Loading branch information
sciborrudnicki committed Aug 19, 2023
1 parent b8feda9 commit 99484a6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions math/_index.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@forward 'range' as range-*;
@forward 'math.calculate.function';
@forward 'math.range.function';
@forward 'math.sort.function';
@forward 'math.strip-unit.function';
@forward 'sass:math';
35 changes: 35 additions & 0 deletions math/_math.sort.function.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Sass.
@use 'sass:list';
@use 'sass:math';

// Modules.
@use '../list/remove/remove.nth.function';

// Status: DONE
// The `list.sort()` function sort `$numbers` in ascending or descending order.
// @param `$sort` The order ascending(asc) or descending(desc) which `$numbers` are sorted.
// @arbitrary `$numbers...` Numbers to `$sort` in ascending(asc) or descending(desc) order.
// @returns The returned value is sorted list `$numbers` in order `$sort` ascending(asc) or descending(desc).
@function sort($sort, $numbers...) {
@if list.length($numbers) > 0 {
$result: ();
$do: true;
@while $do == true {
$number: if($sort == desc, math.max($numbers...), math.min($numbers...));
$index: list.index($numbers, $number);
$numbers: remove.nth($numbers, $index);
$result: list.append($result, $number);
@if list.length($numbers) == 0 {
$do: false;
}
}
@return $result;
}
@return $numbers;
}

// Examples.
// $-numbers: (21, 12, 43, 64, 75, 36, 27, 18, 29, 510);

// @debug sort(asc, $-numbers...); // 12 18 21 27 29 36 43 64 75 510
// @debug sort(desc, $-numbers...); // 510 75 64 43 36 29 27 21 18 12

0 comments on commit 99484a6

Please sign in to comment.