Sort List Fields

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

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

Sort List Fields

Post by RossG » Mon Aug 27, 2018 12:12 am

I have two list fields.
When I sort field f2 I want field f1 to be
sorted in the same order.

Stack attached.
Attachments
List Sort.zip
(1.4 KiB) Downloaded 220 times
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Sort List Fields

Post by bogs » Mon Aug 27, 2018 1:11 am

I must be missing something incredibly obvious here, but I'm not sure I get the question. Field 1 has whole numbers, field 2 has fractional components.
List Sort_003.png
List Sort_003.png (8.93 KiB) Viewed 6445 times
Do you mean you want field 1 sorted with tabs instead of commas? In columns instead of lines? Something else?

*Edit - just in case you just wanted them sorted, which I seriously doubt, you could try this -

Code: Select all

on mouseUp
   set itemDel to comma
   sort lines of field f1 ascending numeric by word 1 of each
end mouseUp
Last edited by bogs on Mon Aug 27, 2018 1:18 am, edited 1 time in total.
Image

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

Re: Sort List Fields

Post by RossG » Mon Aug 27, 2018 1:17 am

Field 2 shows the stdDev and avgDev of the
sets of numbers in field 1 so when the deviations
are sorted I want the number sets to be in the
same order.
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

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

Re: Sort List Fields

Post by dunbarx » Mon Aug 27, 2018 2:11 pm

Ross.

I also do not understand your question. The short script you have in btn "sort f1" does exactly what you ask it. The entire field is sorted numeric.

Is it that you want to isolate and sort the lines individually, line by line, and not as a "group"?

Craig Newman

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Sort List Fields

Post by SparkOut » Mon Aug 27, 2018 2:20 pm

No, I think the request is for a sort that correlates the index of field 1 with field 2. So when the sort order changes in field 2, the lines of field 1 change sort order to match.
I'd play with this more but on phone, away on hols.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Sort List Fields

Post by bogs » Mon Aug 27, 2018 2:35 pm

I think SparkOut has it right, I hope Ross confirms (or not).

*Edit - I am sure this is not the most efficient way, BUT just in case that is what he's looking to do, here is one way I came up with that seems to work.

Code: Select all

on mouseUp
# all of this code is in the button that sorts field "f2"...
   set itemDel to tab
// first I added a marker to the lines as they originally sit...
   repeat with x=1 to the number of lines of field "f2"
      add 1 to tmpLineNum
      put space & tmpLineNum after line x of field "f2"
   end repeat
   
// after the lines are marked, the sort runs...
   sort lines of field f2 ascending numeric by item 2 of each
   sort lines of field f2 ascending numeric by item 1 of each
   
// I moved the field "f1" into a variable, then cleared the field...
   put field "f1" into tmpFieldOne
   put "" into field "f1"

/* the markers are put in a variable, removed from field "f2", and field "f1" 
lines are moved to those markers...*/
   repeat with x=1 to the number of lines of tmpFieldOne
      put the last character of line x of field "f2" into tmpMoveList
      delete the last character of line x of field "f2"
      put line x of tmpFieldOne into line tmpMoveList of field "f1"
   end repeat
end mouseUp
List Sort_003.png
Original set...
List Sort_003.png (9.53 KiB) Viewed 6353 times
List Sort_004.png
After sort...
List Sort_004.png (9.02 KiB) Viewed 6353 times
*Note - the code above is extremely verbose and can be condensed considerably. I'm sure anyone whose been working with this language for any amount of time knows this, but newer people may not.
Image

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

Re: Sort List Fields

Post by dunbarx » Mon Aug 27, 2018 5:23 pm

This is what I meant, that the field as a whole is not sorted numerically, but that each line is, independently.

But I will wait for the OP....

As for sorting line by line:

Code: Select all

on mouseUp
   get fld "f2"
   set the itemDel to tab
   repeat for each line tLine in it
   sort items of tLine ascending numeric
   put tLine & return after accum
end repeat
put accum into fld 2
end mouseUp
LC cannot do the above natively; you have to parse individual lines and work it out.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Sort List Fields

Post by jacque » Mon Aug 27, 2018 6:08 pm

I have a handler that does this, will post when I get back to my Mac.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Sort List Fields

Post by FourthWorld » Mon Aug 27, 2018 6:49 pm

Do they need to be physically separate fields? Might this be one field with three tab-delimited columns?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Sort List Fields

Post by jiml » Mon Aug 27, 2018 10:00 pm

As Reichard suggested:
Might this be one field with three tab-delimited columns?
See attached stack.
List Sort - jl.livecode.zip
one field with three tab-delimited columns
(2.09 KiB) Downloaded 254 times
Jim Lambert

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Sort List Fields

Post by jacque » Mon Aug 27, 2018 10:53 pm

Here's my parallel sort handlers:

Code: Select all

on parallelSort -- parallel sort of several flds by a key field
  put fld 1 into tOrigList -- the fld that determines the sort order
  sort lines of fld 1 
  -- now add the dependent fields:
  sort lines of fld 2 by arrange(each,tOrigList,2)
  sort lines of fld 3 by arrange(each,tOrigList,3)
  -- etc.: add any number of other fields here
end parallelSort

function arrange pContent,pOrigList,pFld
  -- find out what line we are on:
  get lineoffset(pContent,fld pFld)
  -- use the original list to sort:
  return line it of pOrigList
end arrange
This could probably be faster if it used referenced variables or script locals, but that's the idea.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Sort List Fields

Post by jacque » Mon Aug 27, 2018 11:11 pm

I see I have about five versions of parallel sorting. This one by genius Dick Kriesel is pretty cool. It's sorting items, but could be altered to use lines:

Code: Select all

split varB by comma
sort items of varA by varB[itemOffset(each,varA)]
One (much different) parallel sort, done in HyperCard by Brett Sher 24 years ago, takes 37 lines. We've come a long way since then.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”