json sorting with keys?

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
User avatar
celestian
Dragon
Posts: 276
Joined: Mon May 17, 2010 3:29 pm

json sorting with keys?

Post by celestian »

I'm trying to sort a token property before displaying it. Here is the "Skills" property:

Code: Select all

{"Hide In Shadows":{"baseChance":10,"armorMod":0,"racialMod":0,"dexMod":5,"otherMod":0},"Detect Noise":{"baseChance":15,"armorMod":-5,"racialMod":0,"dexMod":0,"otherMod":0},"Climb Walls":{"baseChance":45,"armorMod":-5,"racialMod":0,"dexMod":10,"otherMod":0}}


I've tried

Code: Select all

[h: mySkills = getProperty("Skills")]
[h: mySkills = json.sort(mySkills)]


But it bitches about the sort. Is there a method to sort the way I'm storing those properties? To be clear I want to sort the "Hide in Shadows", "Detect Noise", "Climb Walls"/etc so when I do a foreach on mySkills they are in order alphabetically.

Craig
Great Wyrm
Posts: 2107
Joined: Sun Jun 22, 2008 7:53 pm
Location: Melbourne, Australia

Re: json sorting with keys?

Post by Craig »

celestian wrote:I'm trying to sort a token property before displaying it. Here is the "Skills" property:

Code: Select all

{"Hide In Shadows":{"baseChance":10,"armorMod":0,"racialMod":0,"dexMod":5,"otherMod":0},"Detect Noise":{"baseChance":15,"armorMod":-5,"racialMod":0,"dexMod":0,"otherMod":0},"Climb Walls":{"baseChance":45,"armorMod":-5,"racialMod":0,"dexMod":10,"otherMod":0}}


I've tried

Code: Select all

[h: mySkills = getProperty("Skills")]
[h: mySkills = json.sort(mySkills)]


But it bitches about the sort. Is there a method to sort the way I'm storing those properties? To be clear I want to sort the "Hide in Shadows", "Detect Noise", "Climb Walls"/etc so when I do a foreach on mySkills they are in order alphabetically.


JSON objects are basically javascript objects and don't support sorted field names. So even json.sort() was able to sort it (say as by converting it to a string), once you try access it again and it convers to a json object you would lose the sort order. So if you want to access them in a sorted order say for printing purposes you can do so like the following

Code: Select all

[h: mySkills = getProperty("Skills")]
[h: skillNames = json.sort(json.fields(myskills, 'json'))] <!-- get just the field names and sort them -->
[foreach(sname, skillNames, '<br>'), code: { <!-- loop through fields in order -->
     [h: sk = json.get(myskills, sname)]
     [r: 'Skill ' + sname  + ', base chance = ' + json.get(sk, 'baseChance')]
}]

User avatar
celestian
Dragon
Posts: 276
Joined: Mon May 17, 2010 3:29 pm

Re: json sorting with keys?

Post by celestian »

Worked perfect, thanks for the tip!

Code: Select all


[h: mySkills = getProperty("Skills")]
[h: skillNames = json.sort(json.fields(mySkills, 'json'))]
[foreach(prop, skillNames, ""), code: {
... print out "prop" here in order...
}]


Post Reply

Return to “MapTool”