Sorting second level items alphabetically?

Hi,

Is it possible to select second level items, and then sort those items alphabetically within TaskPaper (i.e., through a script)?

Thanks!

Not quite sure whether you are speaking of

  • selection by script, or
  • preliminary selection through GUI

but there’s an example here of sorting the peers of the currently selected item:

1 Like

Sorry, just catching this now… Basically, if the top level of a section would be Vegetables, and the second level would list them, unordered, it would look like this…

Vegetables

  • Radishes
  • Cucumbers
  • Peppers
  • Lettuce

So, I’m wondering if there’ a way to select the second level items, and then sort them alphabetically (via a script or some other function) so that they’d be re-arranged in the following way…

Vegetables

  • Cucumbers
  • Lettuce
  • Peppers
  • Radishes

That’s what I’m seeking. Thanks!

I think it might then be worth trying the script above.

If you:

  • place your cursor anywhere in the list of vegetables,
  • and run the script.

Then I think it should do what you want.

For installation etc:

[Using Scripts](https://www.taskpaper.com/guide/using-taskpaper/using-scripts.html)

Thanks so much for this!

I tried selecting the whole script, but Script editor didn’t like the " // " at the top, as in: // Sort currently selected node and its siblings by due date.

So, I deleted them, and then whole line, hit Compile, and then I got a Syntax Error that read, " Expected expression but found “>”. "

Is there something I should do to fix this?

Thanks again!

Have you selected JavaScript in the language selector at top left of Script Editor ?

See: [Using Scripts] (https://www.taskpaper.com/guide/using-taskpaper/using-scripts.html)

1 Like

Ah. Sorry about that. No, I just need to sort items in the secondary level.

And yes, I did select the item, and then clicked Compile.

With “JavaScript” selected in the top left language selector of Script Editor ?

Screenshot 2021-03-16 at 20.07.05

1 Like

Got it. It worked!!!

It’s perfect.

Thank you for all of your help with this. Really appreciate it.

1 Like

BTW, just noticed one thing… Turns out, your terrific script tends to work when the first word is capitalized. When it’s not, the script tends not to sort properly. FWIW… Thanks again!

You can adjust the sort, to make the ordering, for example, case-insensitive, by editing the definition of the comparison on which the sort depends.

Given a function like:

// toLower :: String -> String
const toLower = s =>
    // Lower-case version of string.
    s.toLocaleLowerCase();

and tracking down any applications of the comparing function in the source of the script, you could edit from something like:

.sort(comparing(tagValue(strTagName)))

to, for example:

.sort(comparing(toLower(tagValue(strTagName))))

or if you prefer:

.sort(comparing(tagValue(strTagName).toLocaleLowerCase()))

and similarly this:

.sort(
    mappendComparing(
        [ // Sorted by rising date,
            [tagValue(options.tagName), true],
            // then by text AZ
            [x => x.bodyContentString, true]
        ]
    )
);

could be edited, to make it case insensitive, to something like:

.sort(
    mappendComparing(
        [ // Sorted by rising date,
            [tagValue(options.tagName), true],
            // then by text AZ
            [x => x.bodyContentString.toLocaleLowerCase(), true]
        ]
    )
);

( I personally use case-sensitive sorting, so I won’t make an edit myself, but the source is there for you to tailor to your own needs )

1 Like

Thank you so much for all of this! I haven’t yet made these changes, but I’ll circle back and let you know how it goes.

BTW, are these any kind of script shortcuts in TaskPaper (e.g., icons that one can attach to toolbars or anything like that)? Thanks!

There are various ways of running scripts – the details are here:

[Using Scripts] (https://www.taskpaper.com/guide/using-taskpaper/using-scripts.html)

Thanks! Yes, I’ve read thought that page, and appreciate you sending it to me again. I guess I’m just wondering if there are any kind script - usage shortcuts already built into TaskPaper (e.g., attaching an icon somewhere or if there’s a palette shortcut) or if one has to just rely on FastScripts or Keyboard Maestro to run scripts with keyboard shortcuts. That’s all.

Thanks again!

I think the closest might be the Command Palette described on that page.

Otherwise, as you say – things like Keyboard Maestro, Fastscripts, (and I think now, to some extend Bartender, though I haven’t tried using that to launch scripts)

1 Like

Got it. Many thanks, again, for all of your help… Really appreciate it.