#Is this the best way

1 messages · Page 1 of 1 (latest)

worthy kite
#

Is this code the best way to Sort Json (Later I want to replace that Data List with a Property, But I am trying to figure out the best way top sort it.

[h: data = json.evaluate('{"Backpack":{"Amount":1,"Store":1,"Weight":1},"Traveling Clothes":{"Amount":1,"Store":1,"Weight":2},"Waterskin":{"Amount":1,"Store":1,"Weight":0.25},"Glowstone":{"Amount":1,"Store":1,"Weight":0.1},"Healing Potion":{"Amount":0,"Store":0,"Weight":0.1},"Lucky Rabbits Foot":{"Amount":1,"Store":1,"Weight":0.1},"Golden Idol":{"Amount":1,"Store":1,"Weight":0.25},"Katana":{"Amount":1,"Store":1,"Weight":4},"Kapi Coins":{"Amount":5,"Store":1,"Weight":0.25},"Copper Ring":{"Amount":1,"Store":1,"Weight":0.1},"Magic Elixer":{"Amount":3,"Store":1,"Weight":0.1},"Choker ofCommunication":{"Amount":1,"Store":1,"Weight":0.1},"Book on Ancient Tantric Sex Tecniques":{"Amount":1,"Store":1,"Weight":1},"Silk Rope":{"Amount":2,"Store":1,"Weight":3},"Short Sword":{"Amount":1,"Store":1,"Weight":3},"Arcane Lance":{"Amount":0,"Store":1,"Weight":0},"Dagger":{"Amount":1,"Store":1,"Weight":2},"Bite":{"Amount":0,"Store":1,"Weight":0},"Claws":{"Amount":0,"Store":0,"Weight":0},"Vampire Serum":{"Amount":100,"Store":1,"Weight":0.01}}')]

[h: keys = json.fields(data)]
[h: list = ""]

[h, foreach(k, keys), code:
{
[h: item = json.get(data, k)]
[h: weight = number(json.get(item, "Weight"))]

[h: list = list + weight + "|" + k + ";"]

}]

[h: sorted = listSort(list, "A", "N")]

[h: output = ""]

[h, foreach(entry, sorted), code:
{
[h: name = listGet(entry, 1, "|")]
[h: output = output + name + ", "]
}]

[r: output]

urban bluff
#

The best function to sort json is json.sort, but it only works with arrays (and an array of objects). So, if you convert your object of objects into an array of objects it becomes usable... e.g.

<!-- your h:data assignment here -->

[h: dataAsArrayOfObjects = ""]
[h, forEach(item, data): dataAsArrayOfObjects = json.append(dataAsArrayOfObjects, json.set(json.get(data, item), "Name", item))]
[r: readSorted = json.path.read(json.sort(dataAsArrayOfObjects, "asc", "Weight", "Name"), "[*]['Name', 'Weight']")]

Depending on your needs, if you were to store it natively as an array of objects, then the conversion step becomes unnecessary.

hallow blaze
#

With item lists, it's good to have a unique itemID just in case you want to store items with the same name as a different item.

worthy kite
#

Basically I want to have it stored on a Token, then when a Player updates the List, it will auto Sort it alphabetically. The Data I showed in my Examplke is just a list from my Property "Expendables"

This is the Code I use to update and creat the Item for storage.
[h: expendableNew = json.set("{}", "Name", name, "Amount", amount, "Store",Storage, "Weight",Weight)]
[h: Expendables = json.set(Expendables, name, expendableNew)]
}]

urban bluff
#

How/where do players update the list? When/where does it need to show as sorted?

Also with the supplied update/create script above, you may need to additionally handle updating an existing item's name (so it does not create a duplicate) and also handle creating a new item with the same name as an existing one (so it does not overwrite the existing one). See aliasmask's suggestion above for one approach.

worthy kite
#

[h,if(json.isEmpty(Expendables)): mpList = ""; mpList = json.fields(Expendables)]

 [name = "---"]
 [amount = 0 ]
 [Weight = 0 ]
 [Storage = 0 ]

[h: status=input(
"name|"+name+"|Name|TEXT",
"amount|"+amount+"|Amount|TEXT",
"Weight|"+Weight+"|Item Slot Per Item|TEXT",
"Storage|0|Are you Putting this on your Person? Check for yes|CHECK",
"continue|1|Continue adding expendables (uncheck to stop adding)|CHECK"
)]
[h: abort(status)]

[H,If(Storage==1),CODE:{
[H:CurrentSlots=CurrentSlots+(Weight*amount)]
};{
[H:CurrentSlots=CurrentSlots]
}]

[h: expendableNew = json.set("{}", "Name", name, "Amount", amount, "Store",Storage, "Weight",Weight)]
[h: Expendables = json.set(Expendables, name, expendableNew)]
}]

Gained [r: amount] [r: name] and it <BR>
[R,IF(CurrentSlots>Slots):"You are over Incumbered";""]

[h,if(continue),CODE:
{
[macro("AddExpendables@Lib:Test"):""]
};
{
[h, macro("InventoryScreen@Lib:Test"): ""]
}]

This is the Full code to add that I currently have

versed pewter
worthy kite
#

thanks