text sortType

Geoff Canyon gcanyon at gmail.com
Tue May 28 16:37:38 CDT 2013


Interesting -- this works in one line:

   sort lines of x by word 1 to -2 of item 1 of each & char -10 to -1 of
("0000000000" & word -1 of item 1 of each)

I'm a little put off by not using the native "numeric" -- I'd be worried
that something I'm not thinking of right now would break it. But
nevertheless, it works.

I timed three options and found that (on the sample I tried --  1 million
lines)

   sort lines of x numeric by word -1 of item 1 of each
   sort lines of x by word 1 to -2 of item 1 of each

is fastest, barely.

   sort lines of x by word 1 to -2 of item 1 of each & char -10 to -1 of
("0000000000" & word -1 of item 1 of each)

is just slightly slower.

   sort lines of x by sortKey(item 1 of each)

with

function sortKey X
   return ((word 1 to -2 of X) & (char -10 to -1 of ("0000000000" & word -1
of X)))
end sortKey

was slower, but only by about 1.4 times.

gc


On Tue, May 28, 2013 at 3:04 PM, Dar Scott <dsc at swcp.com> wrote:

> Whoops, I didn't read Peter's solution all the way.
>
> I guessed at what he was doing instead of giving it the attention it
> deserved.
>
> I guess my thumbs up was for using the sorting value function and for
> putting in zero digits.
>
> I would (off the top of my head) simplify (and change) that to this:
>
> function reformatLine pL
>   get item 1 of it
>   return (word 1 to -2 of it) && char -6 to -1 of ("000000" & word -1 of
> it)
> end reformatLine
>
> By adding leading zeros for a fixed length, a text sort is the same as a
> number sort.
>
> A 3rd solution is to put fixed length numerals in the last word of the
> original data.
>
> (I could try using the *s as an excuse, but it is not a good excuse, sorry
> for commenting after only a glance.)
>
> Dar
>
>
> On May 28, 2013, at 1:51 PM, Dar Scott wrote:
>
> > I think this and Geoff's are good!  This one is more general if you can
> come up with some sort of metric or sortvalue for each item/list.  Geoff's
> is simpler for this case.  The speed difference will depend on the length
> of the list.
> >
> > Shouldn't the zero be put 'before' instead of 'after' to force a
> numerical sort?
> >
> > Also, there is a shortcut.
> >
> > put char -6 to -1 of ("000000" & tKey) into tKey
> >
> > Dar
> >
> >
> > On May 28, 2013, at 1:41 PM, Peter Haworth wrote:
> >
> >> The following worked for me (with apologies ofr any asterisks that may
> be
> >> inserted into the script by my email client)
> >>
> >> It assumes there won;t be any numbers > 6 digits.
> >>
> >> *on* mouseUp
> >>
> >>  *sort* lines of field "Field" by reformatLine(each)
> >>
> >> *end* mouseUp
> >>
> >>
> >> *function* reformatLine l
> >>
> >>  *local* tKey
> >>
> >>  *put* word 1 to -2 of item 1 of l into tKey
> >>
> >>  *repeat* 6-the length of word -1 of item 1 of l
> >>
> >>     *put* zero after tKey
> >>
> >>  *end* *repeat*
> >>
> >>  *put* word -1 of item 1 of l after tKey
> >>
> >>  *return* tKey
> >>
> >> *end* reformatLine
> >>
> >> Pete
> >> lcSQL Software <http://www.lcsql.com>
> >> _______________________________________________
> >> use-livecode mailing list
> >> use-livecode at lists.runrev.com
> >> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> >> http://lists.runrev.com/mailman/listinfo/use-livecode
> >
> >
> > _______________________________________________
> > use-livecode mailing list
> > use-livecode at lists.runrev.com
> > Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> > http://lists.runrev.com/mailman/listinfo/use-livecode
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>


More information about the use-livecode mailing list