a quick question:
is there a(n easy) way to sort json objects?
I know that json.sort does arrays, but is there also something for objects?
is there a way to sort json objects?
Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice
is there a way to sort json objects?
GETTING STARTED WITH MAPTOOLS - TUTORIALS, DOCS, VIDEOS, TOOLS, ETC
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS
Re: is there a way to sort json objects?
Nope, I don't think so.
You could maybe make a udf, though.
use json.fields() to get a list, sort the list and create a new object by calling the original values in that order. I'm assuming the order of the object is reflective of the order the fields are added, though this may not be true.
You could maybe make a udf, though.
use json.fields() to get a list, sort the list and create a new object by calling the original values in that order. I'm assuming the order of the object is reflective of the order the fields are added, though this may not be true.
Drop-In Utilities:
My Spell Manager for D&D3.5 and PFRPG
My Inventory Manager for D&D and PFRPG, but more generally useable than that.
My Message Manager -- My Top-Down D&D Token Images
and my Custom Initiative & Status/Spell-Effect Tracker (work in progress, but functional).
My Spell Manager for D&D3.5 and PFRPG
My Inventory Manager for D&D and PFRPG, but more generally useable than that.
My Message Manager -- My Top-Down D&D Token Images
and my Custom Initiative & Status/Spell-Effect Tracker (work in progress, but functional).
Re: is there a way to sort json objects?
There's an example on the wiki of a UDF for Sorting Nested JSON Objects (by sub-fields), if that's what you're looking for.
Look under JSON Sorting for the code.
Look under JSON Sorting for the code.
"The trouble with communicating is believing you have achieved it"
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]
Re: is there a way to sort json objects?
I'm not sure and you may want to test, but I think the order remains the same once you set a keyword. So, if your object doesn't change too much, adding and removing keys, then you could use json.fields and return it as a json array. Then use json.sort on the array. Use a foreach to write a new obj in your needed order and replace the old obj with the new obj. Then it will remain sorted and you won't have to keep using json.fields and json.sort. You can put the obj in to foreach and it should be in the correct order.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: is there a way to sort json objects?
thnx for the suggestions. I feared that there was no easy way, but creating a udf shouldn't be too hard. Its only the fields I wanted sorted not the nested objects.
GETTING STARTED WITH MAPTOOLS - TUTORIALS, DOCS, VIDEOS, TOOLS, ETC
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS
-
- Dragon
- Posts: 317
- Joined: Fri Apr 23, 2010 8:17 pm
Re: is there a way to sort json objects?
I don't believe there are any guarantees, so even if you manage to find some hack to get it to work now, there may be a point where it stops working in the future. I'd look at WHY you want to sort them, and that might give you a clue as to where and how to do it. You may be able to do it at the point where you are retrieving and using the values from the object, or you may want to add some kind of special field that holds (a cached array of) the sorted names of the other fields.
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton
Re: is there a way to sort json objects?
i need it because I regularly output the fields to screen, currently i just solved it by listSort(json.fields(etc)) however as changes are much less frequent than screenupdates its faster if they stored sorted, hence my question. Though a sotred in chache is also an interesting idea.
GETTING STARTED WITH MAPTOOLS - TUTORIALS, DOCS, VIDEOS, TOOLS, ETC
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS