【问题标题】:Livecode: Sort an Array and Display ItLivecode:对数组进行排序并显示它
【发布时间】:2015-01-02 13:12:30
【问题描述】:

如果我在一个数组中有五个数值,如何在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

【问题讨论】:

  • 到目前为止您尝试过什么?您可以发布您尝试过的代码。这不是您请求代码的地方。
  • 我编辑了我的问题@SridharDD

标签: arrays sorting livecode


【解决方案1】:

怎么样:

sort lines of it numeric descending by tNum[each]

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

【讨论】:

  • 如何使用您提供的代码进行输出?我怎么称呼它?
  • 如果你使用与之前相同的代码,但将排序切换到上面的行。
  • 没有显示数组值...它显示... Sorted in ascending order: ], ], ], ], ]; Sorted in descending order: , , , ,
  • 嗯,基于 num 是 LC 中的保留字。 Young 把你所有的 num[] 改成 tNum[]
【解决方案2】:

在 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

【讨论】:

    猜你喜欢
    • 2020-08-09
    • 1970-01-01
    • 2021-07-23
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    相关资源
    最近更新 更多