is it possible to sort data using ioRedis without using js' functions? I have this kinda get function which is supposed to sort authors by their age (year).
When I create an author, I do something like this:
http localhost:5000/authors id=1 name="name" lastname="lastname" year=1970
I need to do it using JSON.stringify because if I don't, then under they key there is [object Object]
Then I parse each of stringified, because keys look like this:
[
'{"id":"2","name":"autor","lastname":"ch","year":"1998"}',
'{"id":"5","name":"autor5","lastname":"ch","year":"2005"}',
'{"id":"4","name":"autor4","lastname":"ch","year":"2002"}',
'{"id":"3","name":"autor3","lastname":"ch","year":"1997"}'
]
My code:
router.get("/", async (req, res) => {
const keys = await client.smembers("authors")
console.log(keys)
const keysMapped = keys.map(key => JSON.parse(key))
const sorted = await client.sort(keysMapped.year)
//console.log(typeof keysMapped)
//console.log(sorted)
return res.send(sorted);
});
router.post("/", async (req, res) => {
try {
const wynik = await client.hset("authors", JSON.stringify(req.body))
return res.send(req.body)
} catch (err) {
console.error(err)
}
});
The sort function doesn't seem to work, but I don't know how to try in in any other way. There is SORT in Redis, but I don't know if it's available in ioRedis also or I should use maybe Lodash or pure JS?