Loading ...
Sorry, an error occurred while loading the content.

96925Re: Vim Tips wiki celebrates with a CSV tip

Expand Messages
  • Tony Mechelynck
    Oct 8, 2008
      On 09/10/08 06:19, John Beckett wrote:
      > Can anyone point out the bug in the code below? (This encapsulates the sorting
      > problem of the CSV tip reported by nh_wzg).
      >
      > Steps:
      > - Copy below to a file, say test.txt.
      > - Yank the second para (copy the code).
      > - Type :@" and press Enter to execute the code.
      > - Press F1 to call MySort() to sort the first para.
      >
      > Problems:
      > - Does not sort lines into any recognisable order.
      > - Changes every time you press F1 (sort is not stable).
      >
      > Is this due to MyCompare sometimes comparing numbers and strings? Any fix?
      >
      > ---start test.txt---
      > 0100
      > 0012
      > 0009
      > 0123
      > BAAA
      > ACCC
      > ABCD
      > BDAA
      > BAAA
      > ACCC
      > ABCD
      > BDAA
      > 0100
      > 0012
      > 0009
      > 0123
      >
      > " Sort lines in first paragraph.
      > map<F1> :call MySort()<CR>
      > function! MySort()
      > normal! gg}k
      > call setline(1, sort(getline(1, '.'), function('MyCompare')))
      > endfunction
      > function! MyCompare(line1, line2)
      > return a:line1> a:line2
      > endfunction
      > ---end---
      >
      > John

      The compare function should return zero for equal, positive for
      disordered, negative for ordered. You're returning respectively 0, 1, 0.
      Try this:

      function MyCompare(line1,line2)
      return (a:line1 > a:line2) - (a:line1 < a:line2)
      endfunction


      Best regards,
      Tony.
      --
      hundred-and-one symptoms of being an internet addict:
      205. You're constantly yelling at your spouse, family, roommate, whatever,
      for using the phone for stupid things...like talking.

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Show all 25 messages in this topic