Skip to content

Instantly share code, notes, and snippets.

@justin2004
Created January 19, 2024 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justin2004/e4ea60ca0e3c8a9cf77ecec25b4a6eef to your computer and use it in GitHub Desktop.
Save justin2004/e4ea60ca0e3c8a9cf77ecec25b4a6eef to your computer and use it in GitHub Desktop.
`sort -n` in APL
# `sort -n` will treat as many leading characters as digits as it can and it will do a numeric sort with whatever it was
# able to treat as a number
#
# e.g.
$ echo -e '123 file.txt\n65 exit.c\n465 words.rtf'
123 file.txt
65 exit.c
465 words.rtf
$ echo -e '123 file.txt\n65 exit.c\n465 words.rtf' | sort -n
65 exit.c
123 file.txt
465 words.rtf
# here is an attempt to do something similar in Dyalog APL
#########################
(⊂⍋m)⌷m←↑'0' '1' '2' '3' '4' '5' '6' '7' '8' '9'∘{leadingNumber←⍵/⍨mask←∧\⍺∊⍨⍵ ⋄ (⍎leadingNumber) (⍵/⍨~mask)}¨'123 file.txt' '65 exit.c' '465 words.rtf'
┌───┬──────────┐
│65 │ exit.c │
├───┼──────────┤
│123│ file.txt │
├───┼──────────┤
│465│ words.rtf│
└───┴──────────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment