diff --git a/extensions/Skyhigh173/json.js b/extensions/Skyhigh173/json.js index 5bceb7485a..8fe25e8f4c 100644 --- a/extensions/Skyhigh173/json.js +++ b/extensions/Skyhigh173/json.js @@ -454,6 +454,24 @@ }, }, }, + "---", + { + opcode: "json_array_sort", + blockType: Scratch.BlockType.REPORTER, + text: "sort array [list] in [order] order", + disableMonitor: true, + arguments: { + list: { + type: Scratch.ArgumentType.STRING, + defaultValue: + "[5.23, 214, 522, 61, 5.24, 62.2, 1, 51212, 0, 0]", + }, + order: { + type: Scratch.ArgumentType.STRING, + menu: "sort_order", + }, + }, + }, makeLabel("Lists"), { opcode: "json_vm_getlist", @@ -498,6 +516,10 @@ acceptReporters: true, items: ["=", "≠"], }, + sort_order: { + items: ["ascending", "descending"], + acceptReporters: true, + }, }, }; } @@ -927,6 +949,21 @@ } return ""; } + + json_array_sort(args) { + let list; + try { + list = JSON.parse(args.list); + } catch { + return ""; + } + if (!Array.isArray(list)) { + return ""; + } + list.sort(Scratch.Cast.compare); + if (args.order === "descending") list.reverse(); + return JSON.stringify(list); + } } Scratch.extensions.register(new JSONS()); })(Scratch);