Skip to content
This repository was archived by the owner on Jan 1, 2026. It is now read-only.

Commit 7734027

Browse files
committed
Updated documentation on SORT
1 parent ed8d60e commit 7734027

2 files changed

Lines changed: 55 additions & 5 deletions

File tree

docs/v2/global_module.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,20 +528,44 @@ For a record, this is a no-op.
528528

529529

530530
### SORT
531+
531532
`( array -- array )`
532533

533-
`( record -- record )`
534+
Sorts elements of an array by a default comparison.
535+
536+
Example:
534537

535-
Sorts elements of an array by a default comparison. For a record, this is a no-op.
538+
```
539+
[3 1 2 4] SORT # [1 2 3 4]
536540
541+
["beta" "gamma" "alpha"] SORT # ["alpha" "beta" "gamma"]
542+
```
537543

538544
### SORT-w/FORTHIC
545+
539546
`( array forthic -- array )`
540547

541-
`( record forthic -- record )`
548+
Sorts elements of an array using a `forthic` string to compare them. This is useful if you
549+
want to sort values in a custom way, or if you want to sort an array of records.
550+
551+
Example:
552+
553+
```
554+
# Sorting by risk
555+
: RISK>ORDER [
556+
["red" 1]
557+
["yellow" 2]
558+
["green"] 3]
559+
] REC SWAP REC@;
560+
561+
["yellow" "red" "green" "red"] "RISK>ORDER" SORT-w/FORTHIC # ["red" "red" "yellow" "green"]
562+
563+
# Sorting by assignee
564+
: TICKET>ASSIGNEE "Assignee" REC@;
565+
566+
TICKETS "TICKET>ASSIGNEE" SORT-w/FORTHIC
567+
```
542568

543-
Sorts elements of an array using a `forthic` string to return a value for comparison. For
544-
a record, this is a no-op.
545569

546570
### SORT-w/KEY-FUNC
547571
`( array key_func -- array )`

docs/v3/global_module.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ For a record, this is a no-op.
468468

469469

470470
### SORT
471+
471472
`( array -- array )`
472473

473474
Sorts elements of an array by a default comparison. For a record, this is a no-op.
@@ -479,6 +480,31 @@ If `<comparator> !COMPARATOR` is called just before `SORT`, the specified `compa
479480
used to compare elements for sort. The `comparator` may be a Forthic string or it may be a
480481
function.
481482

483+
Example:
484+
485+
```
486+
# Sorting with default comparator
487+
[3 1 2 4] SORT # [1 2 3 4]
488+
489+
["beta" "gamma" "alpha"] SORT # ["alpha" "beta" "gamma"]
490+
491+
492+
# Sorting by risk using a comparator
493+
: RISK>ORDER [
494+
["red" 1]
495+
["yellow" 2]
496+
["green"] 3]
497+
] REC SWAP REC@;
498+
499+
["yellow" "red" "green" "red"] "RISK>ORDER" !COMPARATOR SORT # ["red" "red" "yellow" "green"]
500+
501+
502+
# Sorting by assignee using a comparator
503+
: TICKET>ASSIGNEE "Assignee" REC@;
504+
505+
TICKETS "TICKET>ASSIGNEE" !COMPARATOR SORT
506+
```
507+
482508

483509
### FIELD-KEY-FUNC
484510
`( field -- key_func )`

0 commit comments

Comments
 (0)