OK, first of all I am very new to Nim. I have JSON data that I am retrieving via a REST API and I want to sort it based on a field in that data. The data looks something like this:
[{"id":"582090251837636960","name":"Company 1","url":"https://website.com/LeauObI/manage/organization/overview"}, {"id":"602919400114225555","name":"Company 10","url":"https://website.com/o/2pFpYchb/manage/organization/overview"}, {"id":"626000348204499553","name":"Company 3","url":"https://website.com/o/2r9-dbWb/manage/organization/overview"}]
I need to be able to sort this data by the "name" field. I have looked at the sort() function, but have not been able to figure out what needs to be done to get this data sorted. Has anyone done this or something similar?
Thanks in advance.
import json, algorithm
let j = parseJson"""[{"id":"5","name":"A"}, {"id":"6","name":"C"}, {"id":"3","name":"B"}]"""
let x = j.getElems.sorted(proc (a, b: JsonNode): int = cmp(a["name"].getStr, b["name"].getStr))
echo x # @[{"id": "5", "name": "A"}, {"id": "3", "name": "B"}, {"id": "6", "name": "C"}]