Sorting large numbers

22 views
Skip to first unread message

Igbanam Ogbuluijah

unread,
Dec 5, 2025, 4:00:48 AM (9 days ago) Dec 5
to vim...@googlegroups.com
Hi all,

It's the time of the year when I use Vimscript to do what it was not designed for: AdventOfCode.

For today's problem, I notice that `:h sort()`, with a Funcref, breaks for large numbers — "breaks" here means that it does not keep to contract in the docs, not that it throws an exception. Since it's not really an exception, I don't think I can file it as a bug… so I'm bringing it here. Maybe someone can help me catch what I'm doing wrong.

In Vimscript…

:echo [(229539777187355, 229539777187355), (487766135067138, 491977135306566), (188325333471071, 188931909913550), (264028451845520, 265514296554744), (245727634348687, 249469249579525), (375117820166731, 378942174241518), (535474757750378, 535849288071548)]->sort({a, b -> a[0] - b[0]})

yields

[(229539777187355, 229539777187355), (487766135067138, 491977135306566), (188325333471071, 188931909913550), (264028451845520, 265514296554744), (245727634348687, 249469249579525), (375117820166731, 378942174241518), (535474757750378, 535849288071548)]

This is the same list because I got it from runtime working on the larger list.

Looking closely, I noticed that the third range is smaller than the first range. This got me thinking about things like overflow. But I checked and `v:numbermax == 9223372036854775807` which is bigger than all the numbers here.

This same operation in Python and Ruby yield the same results different from Vimscript's

In Python

sorted([(229539777187355, 229539777187355), (487766135067138, 491977135306566), (188325333471071, 188931909913550), (264028451845520, 265514296554744), (245727634348687, 249469249579525), (375117820166731, 378942174241518), (535474757750378, 535849288071548)])

In Ruby

[[229539777187355, 229539777187355], [487766135067138, 491977135306566], [188325333471071, 188931909913550], [264028451845520, 265514296554744], [245727634348687, 249469249579525], [375117820166731, 378942174241518], [535474757750378, 535849288071548]].sort { |a, b| a[0] - b[0] }

…both yield

[(188325333471071, 188931909913550), (229539777187355, 229539777187355), (245727634348687, 249469249579525), (264028451845520, 265514296554744), (375117820166731, 378942174241518), (487766135067138, 491977135306566), (535474757750378, 535849288071548)]

Is there some way to use sort that I'm missing here?


Igbanam

Igbanam Ogbuluijah

unread,
Dec 5, 2025, 4:11:17 AM (9 days ago) Dec 5
to vim...@googlegroups.com
I am realizing you can reduce the problem to just the first three entries, so you don't have to copy much when reproducing

:echo [(229539777187355, 229539777187355), (487766135067138, 491977135306566), (188325333471071, 188931909913550)]->sort({a, b -> a[0] - b[0]})


Igbanam

Yegappan Lakshmanan

unread,
Dec 5, 2025, 12:25:53 PM (9 days ago) Dec 5
to vim...@googlegroups.com
Hi,

On Fri, Dec 5, 2025 at 1:11 AM Igbanam Ogbuluijah <xigb...@gmail.com> wrote:
I am realizing you can reduce the problem to just the first three entries, so you don't have to copy much when reproducing

:echo [(229539777187355, 229539777187355), (487766135067138, 491977135306566), (188325333471071, 188931909913550)]->sort({a, b -> a[0] - b[0]})


This is a bug in the code.  I will create a PR this weekend.

- Yegappan

Igbanam Ogbuluijah

unread,
Dec 6, 2025, 7:58:34 AM (8 days ago) Dec 6
to vim...@googlegroups.com
Oh awesome.

Could you please tag @igbanam in the bug, so I see what's changing?

Thanks!


Igbanam


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/CAAW7x7ki7k4djawX6-4DkJjx7Vy2dFyy9QbD9Q8doNwNvJ3Ffg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages