Livecode:排序数组并显示它

Livecode: Sort an Array and Display It

如果我在一个数组中有五个数值,如何在 livecode 中按升序和降序对其进行排序?

(添加和更新代码)

我可以按升序排列,但降序呢?

put 1 into num[1]
put 5 into num[2]
put 3 into num[3]
put 2 into num[4]
put 4 into num[5]

local tSortedArray
local tNextIndex

get the keys of num
sort lines of it by num[each]

split it by return

put 1 into tNextIndex
repeat for each element tIndex in it
put num[tIndex] into sorted[tNextIndex]
add 1 to tNextIndex
end repeat

answer "Sorted in ascending order: "&sorted[1]&", "&sorted[2]&", "&sorted[3]&", "&sorted[4]&", "&sorted[5]&"; Sorted in descending order: "&sortedD[1]&", "&sortedD[2]&", "&sortedD[3]&", "&sortedD[4]&", "&sortedD[5]

预期输出:

Sorted in ascending order: 1, 2, 3, 4, 5; Sorted in descending order: 5, 4, 3, 2, 1

怎么样:

sort lines of it numeric descending by tNum[each]

顺便说一下,"num" 在 LiveCode 中保留为数字的缩写,因此您需要将 num[1] 等更改为其他内容才能使您的代码正常工作。

在 LiveCode 中对数组进行排序可能有点繁琐。我会将该数组转换成一个列表来进行排序,如下所示:

0) 假设 tNum 是要排序的数组

1) 将数组转换为列表:

combine tNum using comma

2) 将此列表升序排序:

sort items of tNum ascending numeric

或降序:

sort items of tNum descending numeric

3) 将该列表转换回数组:

split tNum using comma