Sort

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Sort

Post by RossG » Tue May 23, 2017 2:40 am

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
Attachments
Sort.png
Sort.png (11.08 KiB) Viewed 5965 times
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Sort

Post by [-hh] » Tue May 23, 2017 7:24 am

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
shiftLock happens

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Sort

Post by Thierry » Tue May 23, 2017 8:03 am

[-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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Sort

Post by [-hh] » Tue May 23, 2017 8:13 am

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).
shiftLock happens

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Sort

Post by Thierry » Tue May 23, 2017 8:42 am

[-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
Last edited by Thierry on Tue May 23, 2017 1:58 pm, edited 1 time in total.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Sort

Post by [-hh] » Tue May 23, 2017 10:08 am

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.
shiftLock happens

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Sort

Post by Thierry » Tue May 23, 2017 2:00 pm

[-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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Sort

Post by dunbarx » Tue May 23, 2017 4:00 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Sort

Post by FourthWorld » Tue May 23, 2017 4:11 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Sort

Post by RossG » Wed May 24, 2017 4:30 am

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.
Attachments
allSets Sort.zip
(2.03 KiB) Downloaded 190 times
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”