## 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: Klaus, FourthWorld, heatherlaine, kevinmiller

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

### Sort List Fields

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
Is age an excuse? Eighty-three and counting.

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

### Re: Sort List Fields

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 (8.93 KiB) Viewed 2705 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.

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

### Re: Sort List Fields

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-three and counting.

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

### Re: Sort List Fields

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: 2359
Joined: Sun Sep 23, 2007 4:58 pm

### Re: Sort List Fields

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: 4880
Joined: Sat Feb 25, 2017 10:45 pm

### Re: Sort List Fields

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"
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
``````
Original set...
List Sort_003.png (9.53 KiB) Viewed 2613 times
After sort...
List Sort_004.png (9.02 KiB) Viewed 2613 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.

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

### Re: Sort List Fields

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
Posts: 5790
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

### Re: Sort List Fields

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
Posts: 8301
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

### Re: Sort List Fields

Do they need to be physically separate fields? Might this be one field with three tab-delimited columns?
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com

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

### Re: Sort List Fields

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
Jim Lambert

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

### Re: Sort List Fields

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
Posts: 5790
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

### Re: Sort List Fields

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