HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //home/ubuntu/neovim/test/functional/legacy/function_sort_spec.lua
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local eq = t.eq
local neq = t.neq
local eval = n.eval
local clear = n.clear
local source = n.source
local exc_exec = n.exc_exec

describe('sort', function()
  before_each(clear)

  it('numbers compared as strings', function()
    eq({ 1, 2, 3 }, eval('sort([3, 2, 1])'))
    eq({ 13, 28, 3 }, eval('sort([3, 28, 13])'))
  end)

  it('numbers compared as numeric', function()
    eq({ 1, 2, 3 }, eval("sort([3, 2, 1], 'n')"))
    eq({ 3, 13, 28 }, eval("sort([3, 28, 13], 'n')"))
    -- Strings are not sorted.
    eq({ '13', '28', '3' }, eval("sort(['13', '28', '3'], 'n')"))
  end)

  it('numbers compared as numbers', function()
    eq({ 3, 13, 28 }, eval("sort([13, 28, 3], 'N')"))
    eq({ '3', '13', '28' }, eval("sort(['13', '28', '3'], 'N')"))
  end)

  it('numbers compared as float', function()
    eq({ 0.28, 3, 13.5 }, eval("sort([13.5, 0.28, 3], 'f')"))
  end)

  it('ability to call sort() from a compare function', function()
    source([[
      function Compare1(a, b) abort
        call sort(range(3), 'Compare2')
        return a:a - a:b
      endfunc

      function Compare2(a, b) abort
        return a:a - a:b
      endfunc
    ]])

    eq({ 1, 3, 5 }, eval("sort([3, 1, 5], 'Compare1')"))
  end)

  it('default sort', function()
    -- docs say omitted, empty or zero argument sorts on string representation
    eq({ '2', 'A', 'AA', 'a', 1, 3.3 }, eval('sort([3.3, 1, "2", "A", "a", "AA"])'))
    eq({ '2', 'A', 'AA', 'a', 1, 3.3 }, eval([[sort([3.3, 1, "2", "A", "a", "AA"], '')]]))
    eq({ '2', 'A', 'AA', 'a', 1, 3.3 }, eval('sort([3.3, 1, "2", "A", "a", "AA"], 0)'))
    eq({ '2', 'A', 'a', 'AA', 1, 3.3 }, eval('sort([3.3, 1, "2", "A", "a", "AA"], 1)'))
    neq(nil, exc_exec('call sort([3.3, 1, "2"], 3)'):find('E474:'))
  end)
end)