动态代码:对数组进行排序并显示它

时间:2015-01-02 13:12:30

标签: arrays sorting 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

2 个答案:

答案 0 :(得分:1)

怎么样:

sort lines of it numeric descending by tNum[each]

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

答案 1 :(得分:0)

在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