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

96922RE: Vim Tips wiki celebrates with a CSV tip

Expand Messages
  • John Beckett
    Oct 8, 2008
    • 0 Attachment
      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


      --~--~---------~--~----~------------~-------~--~----~
      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