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/.*\</^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:
- By default, it sorts on text only after the first match in every line.
- 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 ther
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
:help :sort
, obviously.- I think that's good for now.
Similar challenges
- Sort the VimGolf challenges by popularity: Very similar to this challenge, but the scenario is more believable.
- Sort by your own sum: Looks almost the same, but you have to add the numbers yourself. Nasty, nasty challenge.
- Reconstruct the Sentence: Involves some basic sorting, but it's not the hard part.
- Alphabetize the directory: Explores the sorting of multi-line text blocks.
- Sort and add attributes: Very basic sorting challenge.
- Presidential Sorting: Probably puts too much in one challenge, but it's OK I guess. Interesting solution though.
No comments:
Post a Comment