Re: Sorting? [sorted]
Re: Sorting? [sorted]
- Subject: Re: Sorting? [sorted]
- From: has <email@hidden>
- Date: Fri, 13 Sep 2002 12:01:07 +0100
John W Baxter wrote:
>
At 19:44 +0100 9/12/2002, has wrote:
>
>Since AS has no way of knowing you want to look at that part of the string
>
>differently, you're going to have to do the extra work yourself. There are
>
>two ways of approaching this task:
>
>
Another choice (I looked at the footnote) is what the Perl crowd calls the
>
Schwartzian Transform. If something is hard to sort, you convert it to
>
something easy to sort, sort it, and convert the items in the result back.
Ha, yes thanks.<g> The trouble with vast sweeping generalisations: I was
too busy trying to demonstrate how to make a sort routine that can sort
anything by using callbacks to stop and think about real alternatives. Too
long at the computer results in terminal tunnel vision, I fear. [1]
Emmanuel pretty much suggested a pre-transform too, but without the
post-transform that'd change the data permanently which you probably don't
want.
>
My feeling is that for the problem at hand, that is probably not the best
>
approach...has' probably is. (AppleScript doesn't mash text very well, and
>
the transform method would require two mashings.)
Mine mashes the text as well; it just does it in a different place. In
fact, it's not even optimised for this; if you count the number of times a
value is mashed, it's far more than the algorithm actually requires. But I
was really just trying to demonstrate how to create a more flexible sort
routine: describing ways to optimise it would be a sizeable essay in
itself.;) [Which in turn would have to start by finding a replacement to
the cruddy O(n^2) sorting algorithm provided... and so on.] The usual use
for mine would be sorting lists or records on a given item (which it makes
trivially easy), but I reckoned it'd be good enough to solve this problem
too so I posted it.
If the text mashing is complex, however, it'll be _much_ cheaper to do it
as a separate stage before performing the sort, as you're suggesting.
You've got to do it at some point, so why not while the cost is N, rather
than N^2 or worse? [N=number of items in list]
The painful bit is likely to be the reverse transform: as you say, text
manipulation can be pretty manky in AS, and the reverse transform is likely
to be the more complex of the two (plus it's extra coding) [2]. But I have
an idea how this could be avoided:
- Copy the original list, and transform the copy's items as usual.
- Feed the copy into a sort routine that returns not the sorted items but a
list of their indexes.
- Use those indexes to reorder the original list.
The sort algorithm is a little more expensive, but the final transform is
cheap and your result list is _guaranteed_ to contain the original data (no
reverse transform also means one less thing to screw up).
As a solution it's way too complicated to post here, however, and though
I've written just such a sorting algorithm it isn't really in a fit state
to let loose on the general public. The terminally curious can find it
buried in my tableLib library and are welcome to tinker with it, but should
expect some pretty horrible code.
Or you could just go use Perl... ;)
--
>
Example: we have a bunch of lines like
>
name<tab>Sun Sep 1 09:12:09 2002<tab>other stuff
>
>
We want the major sort key to be the name field, and for each name we want
>
the lines sorted temporally. (We also want the lines to continue to be
>
human readable.)
>
>
When I get a chance, I'm going to rewrite the files to look like
>
name<tab>2002-09-01T09:12:09{and something for the weekday}<tab>other stuff
Sounds like the best solution, if it's available. Fix the workflow, rather
than try to patch it up with complex code solutions. If you're happy with
Unix, you should be able to reformat the date part using strptime and
strftime; if you'd rather do it in AS then the dateLib library on my site
may provide equivalent functions.
>
The change will bail me out of another problem...I haven't been smart
>
enough to write a working sort command for the data-as-it-is
(It's quite doable, but I think you'll be a lot happier with a namechange
instead.)
Cheers,
has
[1] Fortunately there's folk more awake on this list that are happy to
correct me when I'm talking rubbish. Keep up the good work. :)
[2] Might not be so bad if using regexes, but AS lacks built-in ones and a
lot of folk don't know how to use them, so I'm a bit reluctant to suggest
them when folk need a solution asap (though do recommend folk go learn them
in their own time as they're an incredibly valuable tool to have)
--
http://www.barple.pwp.blueyonder.co.uk -- The Little Page of AppleScripts
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.