Using PopClip to sort the complete works of Shakespeare alphabetically (video)

I’ve been trying put PopClip through it’s paces a bit, so here’s a kind a fun video:

Extensions featured: Character Count, Select All, BBEdit, Sort.

Some observations:

  • There’s a couple of seconds delay for PopClip to pop-up. I think a good part of that is the time taken to do the character count. I could dive into this to see if there are any potential optimizations.

  • I’m blown away by how fast the BBEdit extension works with such a large text (that’s a large credit to BBEdit I think!).

  • That ‘Sort’ takes about 12 seconds (and for user experience I’d like it if we saw a spinner there) but it does the trick. Under the hood this is JavaScript’s Array.sort() function. It’s running on an M1 Air btw. I wonder how fast it is on a top end machine.

  • I note that the sort algorithm treats leading white space as significant. I didn’t know that.

  • Also worth noting, BBEdit has its own built in 'Sort Lines" function that takes about a second so this whole exersise is kinda…

And I do now know that there 10 lines of Shakespeare that begin ‘Zounds’:

  • Zounds, how has he the leisure to be sick
  • Zounds, I bleed still, I am hurt to the death.
  • Zounds, I will speak of him, and let my soul
  • Zounds, sir, you are one of those that will not serve God if the devil
  • Zounds, sir, you’re robb’d, for shame put on your gown,
  • Zounds, where thou wilt, lad, I’ll make one. An I do not, call me
  • Zounds, will they not rob us?
  • Zounds, ye fat paunch, an ye call me coward, by the Lord, I’ll stab
  • Zounds, you rogue, you rascal!
  • Zounds! I was never so bethump’d with words
1 Like

Zounds! I was never so bethump’d with words

That’s appropriate! And a line I think I’ll borrow now and again…

Anyway, this is pretty amazing.

1 Like

“I note that the sort algorithm treats leading white space as significant”

Sorting totally depends on the internal representation of characters. A space is a lower ‘number’ (hex 20) than an ‘a’ (hex is 40) for example. My recollection also is that Mac and PC use different character sets (? it has been maybe a decade since I’ve thought about that and am totally a Mac guy - so my memory may be off by a whole bunch. Maybe it was different between PC and mainframe??? In the old days, I used to be a programmer LOL)

I specifically use this to force some files to the top of my finder for example by putting in “_” at beginning of file names.

1 Like

Yes indeed, it makes sense that it would treat leading spaces as significant, since they are just characters after all. I noticed BBEdit’s own sort function has the option whether to ignore leading whitespace or not which is interesting, as well as some other options:

Mac and PC (these days at least) use the same character set, although they still differ in terms of line endings — CR + LF (Windows) vs LF (Mac and Linux).

1 Like