Sort by line length?

1,583 views
Skip to first unread message

Fritz

unread,
Aug 21, 2008, 10:34:22 PM8/21/08
to BBEdit Talk
Does anyone know how to sort lines based on length? For example, how
would you sort:

apple
banana
orange
pear
pineapple

...into:

pineapple
banana
orange
apple
pear

?

TIA,
Fritz

Peter N Lewis

unread,
Aug 22, 2008, 12:35:41 AM8/22/08
to bbe...@googlegroups.com

You could use a script, but one relatively easy, albeit slightly
tedious, way is to do this sequence:

Search and replace ^(.*)$ with \1z<BULLET>\1, where <BULLET> is any
character that never appears in your file, Bullet (option-8) is
usually a good choice.

Next search and replace .(x*z<BULLET>) with x\1. Repeat this N times
where N is the length of the longest line. To reduce this, you can
use two replaces:

.....(x*z<BULLET>) with xxxxx\1
and when it runs out
use the first replacement up to four times.

Next, sort the lines. The z before the bullet ensures the sort order
is descending in length

Next, replace .*z<BULLET> with nothing.

As I say, slightly tedius, but easy.

And the alternative of using a script may be better if you are
script-capable, but this would work, and could be easily done as a
text factory (although I notice Text Factory items cannot be option
dragged in the same factory - however you can create a second dummy
text factory, drag that search and replace that needs repeating to
it, and then drag it back N times).

Enjoy,
Peter.

--
Keyboard Maestro 3 Now Available!
Now With Status Menu triggers!

Keyboard Maestro <http://www.keyboardmaestro.com/> Macros for your Mac
<http://www.stairways.com/> <http://download.stairways.com/>

Bryan

unread,
Aug 22, 2008, 8:49:06 AM8/22/08
to BBEdit Talk
BBEdit is not the best tool for this.

Excel is fast if your list isn't too long. Drop your list into column
A, in cell B1 type "=len(a1)", double-click the fill-handle, then
select columns A and B, press tab to put the cursor in column B, and
hit the "Z->A" reverse sort button. Copy/paste column A back into
BBEdit and you're done.

Or a unix filter in the #! menu (excluding "---" lines):

--------------
#! /usr/bin/perl -w

print sort { $al = length($a); $bl = length($b); $bl <=> $al } <>;
--------------

I love perl. =)

- Bryan

Fritz

unread,
Aug 22, 2008, 11:27:45 AM8/22/08
to BBEdit Talk
Awesome, thanks, guys. I solved it for the time being with a quick
little Filemaker thing, but those other solutions are good to know.

Doug McNutt

unread,
Aug 22, 2008, 8:14:25 PM8/22/08
to bbe...@googlegroups.com
At 08:27 -0700 8/22/08, Fritz wrote:
>Awesome, thanks, guys. I solved it for the time being with a quick
>little Filemaker thing, but those other solutions are good to know.

Some things are fun and some are instructive.

For ASCII text I thought about mentioning a perl filter. You could get the text from BBEdit and read it, line by line into a perl array with a read and push loop

perl's array sorting ability allows you to define your own call back for larger/smaller testing and you could use a one line subroutine like this:

{ len($a) <=> len($b) }

The whole thing would be a dozen lines maximum.

But, . . . . .

Then I though: This is a MacIntish !. He really means to have it all work in a font-aware way that would allow for proportional spacing. "mmm" needs to sort as longer than "iii". I think I could do that with quickdraw calls but I haven't the foggiest idea how it could be accomplished in OS neXt. Given the push to UTF-8 I'm not even sure it would work as text.

--

--> From the U S of A, the only socialist country that refuses to admit it. <--

Reply all
Reply to author
Forward
0 new messages