import { fn } from "https://deno.land/x/ddc_vim@v4.1.0/deps.ts";
const { sort } = fn;
Sort the items in {list} in-place. Returns {list}.
If you want a list to remain unmodified make a copy first:
:let sortedlist = sort(copy(mylist))
When {how} is omitted or is a string, then sort() uses the
string representation of each item to sort on. Numbers sort
after Strings, Lists
after Numbers. For sorting text in the
current buffer use :sort
.
When {how} is given and it is 'i' then case is ignored. In legacy script, for backwards compatibility, the value one can be used to ignore case. Zero means to not ignore case.
When {how} is given and it is 'l' then the current collation
locale is used for ordering. Implementation details: strcoll()
is used to compare strings. See :language
check or set the
collation locale. v:collate
can also be used to check the
current locale. Sorting using the locale typically ignores
case. Example:
" ö is sorted similarly to o with English locale.
:language collate en_US.UTF8
:echo sort(['n', 'o', 'O', 'ö', 'p', 'z'], 'l')
['n', 'o', 'O', 'ö', 'p', 'z']
" ö is sorted after z with Swedish locale.
:language collate sv_SE.UTF8
:echo sort(['n', 'o', 'O', 'ö', 'p', 'z'], 'l')
['n', 'o', 'O', 'p', 'z', 'ö']
This does not work properly on Mac.
When {how} is given and it is 'n' then all items will be sorted numerical (Implementation detail: this uses the strtod() function to parse numbers. Strings, Lists, Dicts and Funcrefs will be considered as being 0). Note that this won't sort a list of strings with numbers!
When {how} is given and it is 'N' then all items will be sorted numerical. This is like 'n' but a string containing digits will be used as the number they represent.
When {how} is given and it is 'f' then all items will be sorted numerical. All values must be a Number or a Float.
When {how} is a Funcref
or a function name, this function
is called to compare items. The function is invoked with two
items as argument and must return zero if they are equal, 1 or
bigger if the first one sorts after the second one, -1 or
smaller if the first one sorts before the second one.
{dict} is for functions with the "dict" attribute. It will be
used to set the local variable "self". Dictionary-function
The sort is stable, items which compare equal (as number or as string) will keep their relative position. E.g., when sorting on numbers, text strings will sort next to each other, in the same order as they were originally.
Can also be used as a method
:
mylist->sort()
Also see uniq()
.
Example:
func MyCompare(i1, i2)
return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1
endfunc
eval mylist->sort("MyCompare")
A shorter compare version for this specific simple case, which ignores overflow:
func MyCompare(i1, i2)
return a:i1 - a:i2
endfunc
For a simple expression you can use a lambda:
eval mylist->sort({i1, i2 -> i1 - i2})