2013-11-14

Sort by sum of numbers in a line(?)

While I'm the author of this challenge, it's not exactly my favorite. I made some mistakes in its design. It's short though, and it's got a couple neat tricks.

:sor!n/.*\</<CR>ZZ

:sor!n/.*\</^MZZ

The point of the challenge is to sort lines, so we clearly need :sort (:sor for short). It's a reverse sort (big to small), so make that :sor!. It's a numeric sort, so we'd better give it the n flag.

Since we're not sorting from the first char in the line, we'll need :sor's // argument, which passes it a regex. It can work two ways:

  1. By default, it sorts on text only after the first match in every line.
  2. You can also pass the r flag, which makes it sort on the text during and after the first match. This can be convenient, but the r flag costs a stroke.

The shortest regex that will match the last word on the line by itself (using the r flag) is \d*$ (or \w*$, or... you can use about half of :help character-classes; take your pick).

The shortest regex that matches up to the last word is .*\<. Since * is greedy, this means "every character in the line until the beginning of the last word". Both regexes are 4 strokes, but this one doesn't use the r flag, so it's the winner.

Read the manual

Similar challenges

No comments:

Post a Comment