Page 1 of 1

Sort List Fields

Posted: Mon Aug 27, 2018 12:12 am
by RossG
I have two list fields.
When I sort field f2 I want field f1 to be
sorted in the same order.

Stack attached.

Re: Sort List Fields

Posted: Mon Aug 27, 2018 1:11 am
by bogs
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 6443 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

Re: Sort List Fields

Posted: Mon Aug 27, 2018 1:17 am
by RossG
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.

Re: Sort List Fields

Posted: Mon Aug 27, 2018 2:11 pm
by dunbarx
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

Re: Sort List Fields

Posted: Mon Aug 27, 2018 2:20 pm
by SparkOut
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.

Re: Sort List Fields

Posted: Mon Aug 27, 2018 2:35 pm
by bogs
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 6351 times
List Sort_004.png
After sort...
List Sort_004.png (9.02 KiB) Viewed 6351 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.

Re: Sort List Fields

Posted: Mon Aug 27, 2018 5:23 pm
by dunbarx
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

Re: Sort List Fields

Posted: Mon Aug 27, 2018 6:08 pm
by jacque
I have a handler that does this, will post when I get back to my Mac.

Re: Sort List Fields

Posted: Mon Aug 27, 2018 6:49 pm
by FourthWorld
Do they need to be physically separate fields? Might this be one field with three tab-delimited columns?

Re: Sort List Fields

Posted: Mon Aug 27, 2018 10:00 pm
by jiml
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

Re: Sort List Fields

Posted: Mon Aug 27, 2018 10:53 pm
by jacque
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.

Re: Sort List Fields

Posted: Mon Aug 27, 2018 11:11 pm
by jacque
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.