Skip to content

Commit

Permalink
Skyhigh173/json: add sort block (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri-kiss authored Aug 25, 2023
1 parent 73c9140 commit b0f8c20
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions extensions/Skyhigh173/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -498,6 +516,10 @@
acceptReporters: true,
items: ["=", "≠"],
},
sort_order: {
items: ["ascending", "descending"],
acceptReporters: true,
},
},
};
}
Expand Down Expand Up @@ -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);

0 comments on commit b0f8c20

Please sign in to comment.