Sorting in array does not work too well

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
Tate83
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 30
Joined: Fri Mar 01, 2013 1:11 am

Sorting in array does not work too well

Post by Tate83 » Fri Jun 14, 2013 11:04 am

Hi all,

Based on this link (http://runrev.screenstepslive.com/s/les ... t-an-array), I created an array sorting function which somehow does not exactly what it should but I cannot see the problem.

I run

Code: Select all

put sortedArray(tArrayIndexFile) into tArrayIndexFileSorted 


with

Code: Select all

function sortedArray @pArray
local tSortedArray
local tNextIndex

# fetch the keys and sort them using the array entry values
get the keys of pArray
sort lines of it by pArray[each]

split it by return

# create a new sorted array using the mapped keys
put 1 into tNextIndex

put pArray[tIndex] into tSortedArray[tNextIndex]
add 1 to tNextIndex
end repeat

return tSortedArray
end sortedArray
(this is exactly the example from the above link).

My data is:
For the original index tArrayIndexFile

Code: Select all

1: Bblanapl, Hans
2: Ablanalp, Henry
3: Abt, Heinrich
4: Abt, Roman
but this somehow ends up being sorted as:

Code: Select all

1. Abt, Heinrich 
2. Abt, Roman 
3. Bbplanalp, Hans
4. Ablanalp, Henri 
while 3 in this case should be after 4.

I cannot spot the problem... Well, while writing this post I just noticed that it actually (seems to) sort by the first names for 3 and 4 as well.
Running LiveCode 6.0.2 on Mac

Thanks,
Pascal

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

Re: Sorting in array does not work too well

Post by dunbarx » Fri Jun 14, 2013 1:46 pm

Hi.

An array variable in LC cannot be trusted to sort the way you want. The data in the array has to be transformed into an ordinary variable and sorted there. As an associated array. only the relationship between the keys and their respective elements is preserved. The order of those components is not.

Craig Newman

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Sorting in array does not work too well

Post by sturgis » Fri Jun 14, 2013 4:20 pm

I got it working by random hit/miss/luck. I distrust using the variable IT in a persistant way like the script uses so I put it into a variable and used that.

Also, I tried and tried and couldn't get it to work in 6.0.2. Tried it in 5.5.4 tweaked a bit, it started working. Saved the stack, tried it in 6.0.2 and it works. So /shrug.

Here is the test code I put into the button:

Code: Select all

local tArray
on mouseUp
-- fill the array
   put "Miller, Kevin" into tArray[1]
   put "Beaumont, Benjamin" into tArray[2]
   put "Kenyon, Oliver" into tArray[3]
   put "Macphail, Ian" into tArray[4]
   put "Pepe, Tio" into tArray[5]

-- call the array sorting function, shove the sorted array into tSorted
   put sortedArray(tArray) into tSorted

-- grab the keys of the now sorted array
   put the keys of tSorted into tKeys
   sort tKeys ascending numeric -- to force the output to be in order by key since arrays don't necessarily come out the way they went in
   repeat for each line tKey in tKeys
      put tSorted[tKey] & cr after msg
   end repeat
end mouseUp

function sortedArray @pArray
local tSortedArray
local tNextIndex

# fetch the keys and sort them using the array entry values
put the keys of pArray into myKeys -- decided not to use "it"

sort lines of myKeys by pArray[each] -- sort the keys based on the element values of the array passed in
-- so that the keys are in order reflecting the desired sort

-- take the keys and split it into an array to be used as an index
split myKeys by return

# create a new sorted array using the mapped keys
put 1 into tNextIndex -- loop through and create the final sorted array
repeat for each element tIndex in myKeys
put pArray[tIndex] into tSortedArray[tNextIndex]
add 1 to tNextIndex
end repeat

return tSortedArray
end sortedArray

Tate83
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 30
Joined: Fri Mar 01, 2013 1:11 am

Re: Sorting in array does not work too well

Post by Tate83 » Fri Jun 21, 2013 12:49 pm

Hi
Thanks for your example.
I just tried to implement but even though I get another order out of my input, it is not the right one.

What I basically do is generate an index.html file with a lot of names sorted by their name.
since it will be one index per letter, the names need to be sorted by more than one char:

Aalmeir
Acme
Adler
Amrein
...

Is there probably another way to solve this outside of the array?
Otherwise I'll just tweak the initial XML file that is imported to ensure the order is right from the beginning..

Best regards,
Pascal

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Sorting in array does not work too well

Post by sturgis » Fri Jun 21, 2013 1:08 pm

Hmm. it should work, not sure why it isn't. However, if you can just shove the names into a variable as a cr delimited list (say variable name tList) you can then

sort lines of tList ascending -- or descending, though asc dec is optional. Ascending is default.

at which point the names in tList should be in alphabetical order.

Tate83
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 30
Joined: Fri Mar 01, 2013 1:11 am

Re: Sorting in array does not work too well

Post by Tate83 » Fri Jun 21, 2013 3:04 pm

Ok, I gave up trying to sort the array directly - but it is also not necessary.

My tool does two things after reading the XML file:
create a name.html file for each person -> no need to sort the order they are created
create one index.html file where I want the links to the individual name.html pages sorted

So in the routine to create the index.html page I just read all the info for this task out of the array into a variable, then sorted this variable and just used the variable (instead of the array) to create the individual links on the index.html..

Now my routine is drastically slower due to my probably not very nice code.
but since the creation of 280 files and two index files takes 4 instead of 2.5 seconds, I don't really care :-)

thanks for your help

best, Pascal

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”