[X] 100% Soccer team

        [X] 100% Sims, Tim
                [X] 100% physical
                [X] 100% uniform deposit #31

        [  ] 50% Smith, Sam
                [  ] 0% physical
                [X] 100% uniform deposit

        [  ] 0% Comer, Hannah
                [  ] 0% physical
                [  ] 0% uniform deposit #8

        [X] 100% Thornton, Tyler
                [X] 100% physical
                [X] 100% uniform deposit #35

I would like to sort the names and keep the lines below each header with the appropriate header. I did try folding and then sorting, but the fold was ignored and all lines were sorted.


A common solution to this problem is known as the "decorate-sort-undecorate" pattern. Presuming the character "@" is not used in your file and that you do have tabs rather than spaces, you can use something like this:

:g/^    \[/.,'}-s/\n/@/
:2,$s/^\([^%]*%\s*\)\([EMAIL PROTECTED])@/[EMAIL PROTECTED]@
:2,$!sort
:2,$s/^\([EMAIL PROTECTED])@\([EMAIL PROTECTED])@/\2\1@
:%s/@/\r/g

All characters are typed literally, and, according to your file (having actual tabs, not spaces for indentation), that first line has a single tab in it between the caret and the backslash+left-square-bracket.

In this case, you'd actually be doing a decorate-decorate-sort-undecorate-undecorate. The first decorate mashes them into one line, the second decorate pulls the name to the front for sorting purposes. It then sorts the results, swaps the name back into position, and then unmashes them all back into their multi-line format.

It's a bit convoluted, but you can create a mapping/function to do it if you want it regularly. You just have to pick a character that's not in use in your file.

HTH,

-tim (not Sims)




Reply via email to