Previously, in this link I asked for help to make a kind of top of users sorted by their score, using a JSON file defined as
let points = JSON.parse(fs.readFileSync('./points.json', 'utf8'));
With the following format:
{
"242043489611808769":{"points":617,"level":4,"time":1482316617392},
"129213229221019648":{"points":22,"level":0,"time":1482316617609},
"190388892799598592":{"points":214,"level":2,"time":1482316612355}
}
And I did it with the following code:
var points = JSON.parse(json);
var points_array = Object.keys(points).map(
function (clave) {
var elemento = points[clave];
elemento.id = clave;
return elemento;
}
);
points_array.sort(
function(a, b) {
return b.points - a.points;
}
);
However, now I need to order another JSON file, defined as
let lpoints = JSON.parse(fs.readFileSync('./lpoints.json', 'utf8'));
With the following format:
{
"256566731684839428":[
{"ID":"242043489611808769", "points":617},
{"ID":"129213229221019648", "points":22},
{"ID":"190388892799598592", "points":214}
]
}
With 256566731684839428
the ID of the guild
, and each block, the user ID and its score.
var lguild = lpoints[msg.guild.id];
if (lguild === undefined) {
lpoints[msg.guild.id] = []
var newGuild = lpoints[msg.guild.id];
newGuild.push({id:msg.author.id, points: 0});
}else{
var userX = lguild.find( function(element) {
return element.ID === msg.author.id;
});
if(userX === undefined) {
lguild.push({id:msg.author.id, points: 0});
}
}
This is the code that updates the data, in case you need the context and better understand how the code works, although it is explained in this other link .
What I need is, just like my program is now ordering the data from the file ./points.json
(above all), which also orders the data from the file ./lpoints.json
, but only from a certain array ( msg.guild.id
).
I tried to do something similar to the code I use to sort the global user data (points.json), but I do not know how to sort the objects within an array.
Thanks in advance.