Page 1 of 1

Sort

Posted: Tue May 23, 2017 2:40 am
by RossG
I have a field as in the attached.

I want to sort on item 3.

Can't figure out a way to do it.

Help appreciated

Re: Sort

Posted: Tue May 23, 2017 7:24 am
by [-hh]
Is the itemdelimiter tab and you wish to sort lexicographically (co-sorted) numeric by the 8 comma-items of tab-item 3?

Field 1:
(USE "#" instead of tab here only to make it better visible)
19-2#Z#04,05,06,08,09,21,27,31#202
10-1#Z#02,03,08,11,18,24,25,31#207
14-1#Z#01,05,08,11,20,22,25,32#252
13-1#Z#04,06,07,09,18,22,28,32#172

Code: Select all

on mouseUp
  put fld 1 into cntnr
  replace tab with ",=," in cntnr 
  -- 19-2,=,Z,=,04,05,06,08,09,21,27,31,=,202
  repeat with j=12 down to 5 --> 8 comma-items
    sort cntnr numeric by item j of each 
  end repeat
  replace ",=," with tab in cntnr
  put cntnr into fld 1
end mouseUp
Field 1 is now lexicographically ordered (numeric) by the 8 comma-items of tab-item 3
(USE "#" instead of tab here only to make it better visible)
14-1#Z#01,05,08,11,20,22,25,32#252
10-1#Z#02,03,08,11,18,24,25,31#207
19-2#Z#04,05,06,08,09,21,27,31#202
13-1#Z#04,06,07,09,18,22,28,32#172

Re: Sort

Posted: Tue May 23, 2017 8:03 am
by Thierry
[-hh] wrote:Is the itemdelimiter tab and you wish to sort lexicographically (co-sorted) numeric by the 8 comma-items of tab-item 3?
Hallo Hermann,

Few days ago, you asked me about the interest of using replaceText() in a sort command.

I believe this could be one area where it makes sense

Code: Select all

on mouseUp
   put fld 1 into T
   set the itemdelimiter to tab
   sort lines of T numeric by replaceText( item 3 of each, comma, empty)
   put T
end mouseUp
Kind regards,

Thierry

Re: Sort

Posted: Tue May 23, 2017 8:13 am
by [-hh]
Brilliant .
(Although the numbers must have, as it is here the case, the same length. That is, leading zeros where necessary. And no decimals and no negative numbers are allowed).

Re: Sort

Posted: Tue May 23, 2017 8:42 am
by Thierry
[-hh] wrote:Brilliant .
:)

Thanks to LiveCode for the sort command and its regex implementation....
(Although the numbers must have, as it is here the case, the same length.
That is, leading zeros where necessary. And no decimals and no negative numbers are allowed).
True.

This solution is based, as usual, with the provided set of datas of the OP.

Other format of datas, another regex.

Regards,

Thierry

Re: Sort

Posted: Tue May 23, 2017 10:08 am
by [-hh]
Hi Thierry,

that would be good stuff for several minutes of a talk on 'regex etc.' with LiveCode Global 2017.
May I propose you as speaker?

"Basic and advanced examples of using regex within LiveCode script"

Would be very interesting.

Re: Sort

Posted: Tue May 23, 2017 2:00 pm
by Thierry
[-hh] wrote:Hi Thierry,
May I propose you as speaker?

"Basic and advanced examples of using regex within LiveCode script"

Would be very interesting.
Thanks Hermann for your ideas...
need to think about it.

Regards,

Thierry

Re: Sort

Posted: Tue May 23, 2017 4:00 pm
by dunbarx
Hi.

Lots of stuff going on here. But through it all, are you OK?

When you say "sort by item 3..." did you mean the third comma delimited item in the group of comma delimited items? That is, there are four "blocks" of data in your field, so the third item in line 1 would be "06". The way the list is ordered, that is the same item as if you only considered the middle block, which I suspect is what you were thinking. But even in that case, if your data stays in this format, you only need to:

Code: Select all

sort fld 1 numeric by item 3 of each
Now this will sort, for example, "2" before "02", if that is OK. Otherwise you need more thinking.

Craig Newman

Re: Sort

Posted: Tue May 23, 2017 4:11 pm
by FourthWorld
Given that the strings are padded with leading zeros for uniform length, do we need to explicitly coerce numeric evaluation? E.g. this would seem sufficient, no?:

Code: Select all

on mouseUp
   set the itemdel to tab
   sort lines of fld 1 by item 3 of each
end mouseUp

Re: Sort

Posted: Wed May 24, 2017 4:30 am
by RossG
As usual I nearly got there apart from a couple of things
now sorted.

I replaced tabs with commas but

---got "numeric" and "ascending" in the wrong order
---sorted from item 3 to 10 instead of 10 down to 3

Thank you all for your help.

My test stack is attached.