Still trying to sort an array...

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
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Still trying to sort an array...

Post by bjb007 » Sun Jun 29, 2008 11:43 pm

Edit: After making a simple card with
some reporting of results I find that
using the arrays below gets the end
result I was looking for.

So the question now is the interpretation
of the array which still seems obscure to me.

But I suppose I should be satisfied with a result.
-------------------------------------------------------
I have an array....

[1] 22
[2] 88
[3] 78
[4] 80
[5] 102
[6] 96
[7] 77
[8] 122
[9] 114
[10] 118
[11] 169
[12] 159
[13] 158
[14] 105
[15] 76
[16] 52
[17] 81
[18] 52
[19] 132
[20] 82
[21] 110
[22] 106

After I combine and split with sort on
item 1 I get this...

[1] 1,22
[2] 10,118
[3] 11,169
[4] 12,159
[5] 13,158
[6] 14,105
[7] 15,76
[8] 16,52
[9] 17,81
[10] 18,52
[11] 19,132
[12] 2,88
[13] 20,82
[14] 21,110
[15] 22,106
[16] 3,78
[17] 4,80
[18] 5,102
[19] 6,96
[20] 7,77
[21] 8,122
[22] 9,114

and when I sort on item 2 I get this...

6,100
5,106
22,110
13,111
12,112
21,114
9,118
10,122
11,122
8,126
19,136
1,26
16,56
18,56
14,58
15,80
7,81
3,82
4,84
17,85
20,86
2,92

Obviously neither of these is any use.

Any suggestions as to where I've gone wrong
or how to get the result I want?
Life is just a bowl of cherries.

TEDennis
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 41
Joined: Tue Apr 11, 2006 12:36 am

Post by TEDennis » Mon Jun 30, 2008 5:42 am

You need to specify the type of data it's sorting on if you're sorting something other than text.

From the Rev doc:
sort [{lines | items} of] container [direction] [sortType] [by sortKey]

The sortType is one of text, numeric, or dateTime. If you don't specify a sortType, the sort is by text.

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Still trying to sort an array...

Post by bjb007 » Tue Jul 01, 2008 2:20 am

Anything that mentions "sort" empties the array.

The best solution I've found is to copy the
element contents into a (hidden) field and sort lines.

This is OK in some cases but if the element
key indicates the source of the contents
(as it does in my case) this info is lost.

Haven't tried copying the key as an
item eg 1,234 but suppose it would work.
Life is just a bowl of cherries.

TEDennis
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 41
Joined: Tue Apr 11, 2006 12:36 am

Post by TEDennis » Tue Jul 01, 2008 4:15 am

Code: Select all

constant cArrayValues = "22,88,78,80,102,96,77,122,114,118,169,159,158,105,76,52,81,52,132,82,110,106"

on mouseUp
   -- Initialize array to values
   set the itemDelimiter to comma
   put empty into tArray
   put 1 into tArrayIndex
   repeat for each item tNum in cArrayValues
      put tNum into tArray[tArrayIndex]
      add 1 to tArrayIndex
   end repeat
   -- tArray now has the values you specified
   put tArray into tWorkArray -- Put the array in a work variable -- this saves tArray for later use
   combine tWorkArray using return and comma -- tWorkArray is now a variable containing lines
   sort lines of tWorkArray ascending numeric by item 1 of each -- tWorkArray is now sorted by key
   put "Sorted by Key" & cr & tWorkArray
   sort lines of tWorkArray ascending numeric by item 2 of each -- tWorkArray is now sorted by value
   put  cr & "Sorted by Value" & cr & tWorkArray after message box
end mouseUp

TEDennis
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 41
Joined: Tue Apr 11, 2006 12:36 am

Post by TEDennis » Tue Jul 01, 2008 4:19 am

An even cleaner version, without the loop to initialize the array ...

Code: Select all

constant cArrayValues = "22,88,78,80,102,96,77,122,114,118,169,159,158,105,76,52,81,52,132,82,110,106"

on mouseUp
   -- Initialize array to values
   put cArrayValues into tArray
   split tArray by comma
   -- tArray now has the values you specified
   put tArray into tWorkArray -- Put the array in a work variable -- this saves the array for later use
   combine tWorkArray using return and comma -- tWorkArray is now a variable containing lines
   sort lines of tWorkArray ascending numeric by item 1 of each -- tWorkArray is now sorted by key
   put "Sorted by Key" & cr & tWorkArray
   sort lines of tWorkArray ascending numeric by item 2 of each -- tWorkArray is now sorted by value
   put  cr & "Sorted by Value" & cr & tWorkArray after message box
end mouseUp

Post Reply