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

RE: New (?) sort implementation for Vim, beats quick sort [long]

Expand Messages
  • Ron Aaron
    ... I agree, but I would add that the syntax should allow a default sort (perhaps specified in sortorder or something. ... would do the default sorting of
    Message 1 of 17 , May 2, 2003
      > I vote in favor of :sort . Consider the difference
      > between :substitute ,
      > which acts on a range (current line by default) and
      > substitute(), which acts on
      > strings. It would be nuch more convenient to sort lines with
      > an ex command.


      I agree, but I would add that the syntax should allow a 'default' sort (perhaps specified in 'sortorder' or something.

      So:
      :'a,'b sort

      would do the default sorting of 'a,'b whereas
      :'a,'b sort "SortSpecial()"

      would use the 'SortSpecial()' function, as would
      : set sortorder="SortSpecial()"
      :'a,'b sort

      The default sort would be a simple strcmp or stricmp. Given that the slowest part of the entire sort as envisioned here is the compare function, using a sort algorithm which minimizes compares is a good thing...
    • Mikolaj Machowski
      ... Couldn t it be implemented in both ways? Managing : commands in scripts isn t always straightforward. m. -- LaTeX + Vim = http://vim-latex.sourceforge.net/
      Message 2 of 17 , May 2, 2003
        On Fri, May 02, 2003 at 10:13:46AM -0400, Benji Fisher wrote:
        > I think you are wrong about the importance (and difficulty) of design
        > decisions versus implementation. I bet that Bram could implement his
        > choice of sort (or maybe just link to one in the standard library) in an
        > hour. Even more important, an inefficient implementation can be changed in
        > a later release, but once the user interface has been chosen, it is very
        > hard to change it.
        > I vote in favor of :sort . Consider the difference between
        > :substitute , which acts on a range (current line by default) and
        > substitute(), which acts on strings. It would be nuch more convenient to
        > sort lines with an ex command.
        > :.,$sort "default comparison
        > :.,.+5sort "a:1[0] < a:2[0]"

        Couldn't it be implemented in both ways?
        Managing : commands in scripts isn't always straightforward.

        m.
        --
        LaTeX + Vim = http://vim-latex.sourceforge.net/
        Vim-list(s) Users Map: (last change 21 Apr)
        http://skawina.eu.org/mikolaj/vimlist
        Are You There?
      • Benji Fisher
        ... I suppose so. There is setline(), a precedent for having a function that changes the buffer. (I never use it myself. Are there any others?) I guess I
        Message 3 of 17 , May 2, 2003
          Mikolaj Machowski wrote:
          > On Fri, May 02, 2003 at 10:13:46AM -0400, Benji Fisher wrote:
          >
          >> I vote in favor of :sort . Consider the difference between
          >> :substitute , which acts on a range (current line by default) and
          >>substitute(), which acts on strings. It would be nuch more convenient to
          >>sort lines with an ex command.
          >>:.,$sort "default comparison
          >>:.,.+5sort "a:1[0] < a:2[0]"
          >
          > Couldn't it be implemented in both ways?
          > Managing : commands in scripts isn't always straightforward.

          I suppose so. There is setline(), a precedent for having a function that
          changes the buffer. (I never use it myself. Are there any others?) I guess I
          am still willing to use :execute .

          Can you give an example where not having sort() would be particularly
          awkward? How hard would it be to write a Sort() user function as a wrapper for
          :execute "sort" ?

          --Benji Fisher
        • Mikolaj Machowski
          ... Heh. What you wrote is example why sort() would be helpful - it is one step more for dealing with lists - slows execution of code - sometimes big. Vim is
          Message 4 of 17 , May 4, 2003
            On Fri, May 02, 2003 at 09:41:54PM -0400, Benji Fisher wrote:
            > >Couldn't it be implemented in both ways?
            > >Managing : commands in scripts isn't always straightforward.
            > I suppose so. There is setline(), a precedent for having a function
            > that changes the buffer. (I never use it myself. Are there any others?)
            > I guess I am still willing to use :execute .
            > Can you give an example where not having sort() would be particularly
            > awkward? How hard would it be to write a Sort() user function as a wrapper
            > for :execute "sort" ?

            Heh. What you wrote is example why sort() would be helpful - it is
            one step more for dealing with lists - slows execution of code
            - sometimes big. Vim is really powerful but slow. Example - simple
            regexp on big file (ca. 20M) Vim makes 5m, perl 15s.

            m.
            --
            LaTeX + Vim = http://vim-latex.sourceforge.net/
            Vim-list(s) Users Map: (last change 21 Apr)
            http://skawina.eu.org/mikolaj/vimlist
            Are You There?
          • Siemerink, Ben J H (Bernardus)
            Hello, I agree that :sort feels more natural than sort(). Without boosting any possible implementation: I would propose a few standard comparison functions or
            Message 5 of 17 , May 5, 2003
              Hello,


              I agree that :sort feels more natural than sort().

              Without boosting any possible implementation: I would propose a few standard
              comparison functions or options:
              + ignore case
              + reverse
              + numerical

              The ignore case is a must IMHO.

              Yet again there are various ways to do so:
              + commandline options: -i -n -r. The advantage is that you can combine them
              (ie. -nr).
              + standard comparison functions. I suddenly don't remember if they are
              already included in Vim. However, these functions can take arguments that
              make them behave as commandline options.


              Saludos, Ben.
              --
              Ben Siemerink
              Madrid, Spain.
            • Piet Delport
              ... (What about ic ?) ... I have a better proposal: Add an operator that works like Perl s and cmp operators[1]. Besides being very useful in its own
              Message 6 of 17 , May 6, 2003
                On Tue, 06 May 2003 at 08:37:07 +0200, Siemerink, Ben J H (Bernardus) wrote:
                >
                > I agree that :sort feels more natural than sort().
                >
                > Without boosting any possible implementation: I would propose a few standard
                > comparison functions or options:
                > + ignore case

                (What about 'ic'?)

                > + reverse
                > + numerical
                >
                > The ignore case is a must IMHO.
                >
                > Yet again there are various ways to do so:
                > + commandline options: -i -n -r. The advantage is that you can combine them
                > (ie. -nr).
                > + standard comparison functions. I suddenly don't remember if they are
                > already included in Vim. However, these functions can take arguments that
                > make them behave as commandline options.

                I have a better proposal: Add an operator that works like Perl's <=>
                and cmp operators[1]. Besides being very useful in its own right, such
                an operator will allow you to do all of the above simply as:

                :sort 'v:a <=> v:b' " default compare
                :sort 'v:b <=> v:a' " reverse compare
                :sort 'v:a <=># v:b' " force case match, regardless of 'ic'
                :sort 'v:a <=>? v:b' " force ignored case match
                :sort 'v:b+0 <=> v:a' " numeric, reversed

                ...and so on. The operator should be trivial to implement by returning
                the result of strcmp()/stricmp() (or whatever wrapper/replacement Vim
                uses under the hood).


                [1] For those not familiar with Perl, those operators return -1, 0, or
                +1 if their first argument is less than, equal to, or greater than
                their right argument, respectively. They're used all the time in
                comparison expressions such as: sort { lc($a) cmp lc($b) } @list

                --
                Piet Delport
                Today's subliminal thought is:
              Your message has been successfully submitted and would be delivered to recipients shortly.