John Little wrote: > On Friday, August 22, 2014 12:05:33 PM UTC+12, mattn wrote: > > > Bug in sort() function? > > > > See :help sort() > > > > > The sort is stable, items which compare equal (as number or as... > > There is indeed a bug in sort(). The stability of the sort is not relevant. > > I haven't been following the thread too closely, but became interested > on seeing Andy's report of > > :echo sort(['3 0', '3 0 1']) > ['3 0 1', '3 0'] > > I tried > :echo sort(['foo bar','foo']) > ['foo bar', 'foo'] > but > :echo sort(['foobar','foo']) > ['foo', 'foobar'] > > The space makes a difference, because it sorts less than a single quote. Try > :echo sort(['foo','foo&bar','foo(bar']) > ['foo&bar', 'foo', 'foo(bar'] > > Looking in eval.c, to compare list items tv2string is called to > stringify each item. tv2string calls string_quote, which puts single > quotes around each string (and doubles any single quotes in the > string). This effectively adds a single quote to the end of each > string, and if it's being compared to a string that has something > comparing less than a quote, it sorts ahead. > > I don't know why string_quote is called; it clearly is not useful for > this purpose, but may be needed for another.
Oof, I didn't realize the item compare function was changing strings before doing the actual compare. And it allocates a buffer for the result, thus that's expensive. Thanks for digging that up! Just removing the quoting means the sorting of strings vs numbers changes completely. That goes against the documentation. I can work around that, hopefully it doesn't cause trouble in a situation that the tests doesn't catch. -- Don't believe everything you hear or anything you say. /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org /// -- -- You received this message from the "vim_dev" 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_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
