Descending query

Hi,

Im looking to combine 6 database fields, sort them descending and then result which one is first, second third etc and eventually save them in 6 other fields. I am trying but its not working. I dont know how to result them.

let player = $w("#name").text;

  wixData.query("POINTS")
 .descending("points1", "points2", "points3", "points4", "points5", "newPoints")
 .eq("title", player)
 .find()
 .then( (results) => {
 let items = results.items[0];
      console.log("Player ID Found")
 let firstItem = items[1];
 let secondItem = items[2];
 let thirdItem = items[3];
 let fourthItem = items[4];
 let fifthItem = items[5];

      player.points1 = firstItem;
      player.points2 = secondItem;
      player.points3 = thirdItem;
      player.points4 = fourthItem;
      player.points5 = fifthItem;
      wixData.save("POINTS", items)
      console.log("Largest Points Saved")

 } )

Maybe i have to combine ?

let total_points = 0;
 for (let i = 0; i < 5; i++) {
  ????????
 }

I hope i explained clearly what i am seeking.
Thanks!

@Ahmad

Hi Mantas,

If you got it right, you want to retrieve the highest points of all player entries, while keeping in mind that each entry has 6 points.

If that’s the case, then …

// A constant to store the points;
const points = [];

wixData.query('POINTS').eq('player', 'Ahmad').limit(1000).find().then((result) => {
    const entries = result.items;
    
    // Loop through all the items to find the highest ones
    entries.forEach(entry => {
        // Loop through the points of each entry
        for (let i = 1; i <=5; i++) {
            // Add the point to the points array
            points.push(entry[`points${i}`]);
        }
    });
    
    /* Now that we have each and every single point of that player
    we just need to sort the array, then get the highest 5 points */
    entries.sort((a, b) => {
        return b - a;
    });
    
    // Get only the highest 5 points;
    const pointsNum = 5; // Number of points to get.
    const highest_points = points.slice(0, pointsNum);
})

Once you have the highest points of all entries, you can do whatever you want with them, display, store, or just print them to the console, simple, isn’t it?

Hope this helps~!
Ahmad

Hi @ahmadnasriya

This code is actually to count all items. But i need to find one persons 6 points and return only 5 highest points. Then save them to separate fields starting points1 as highest ending points5 as lowest.

Points6 field i am using for “New point entires” only

Thanks!

Ok i think i have managed to do what i need. But im having issues saving the data. @ahmadnasriya could you take a look where is mistake here? And maybe i did in a very complicated way and there is a easier/shorter way to do it?
But it works :slight_smile: Console shows correct results.

let newestpoints = $w("#newpoints").value;
 $w('#dataset2').onReady(() => {
 

console.log("Finding TOP5 points...")
wixData.query('POINTS-PERSONAL')
.eq('title', player)
.find()
.then((result) => {
 const entries = result.items[0];
 
 // Loop through all the items to find the highest ones
 let playerpoints = entries
 let points = [];
    points.push(playerpoints.points1, playerpoints.points2, playerpoints.points3, playerpoints.points4, playerpoints.points5, Number(newestpoints));

 // Sort points as descending order
                        points.sort(function (a, b) {
 return b - a;
 })

 let points11 = 0;
 for (let i = 0; i < 1; i++) {
                            points11 += points[i];
 }
 let points22 = 0;
 for (let i = 1; i < 2; i++) {
                            points22 += points[i];
 }
 let points33 = 0;
 for (let i = 2; i < 3; i++) {
                            points33 += points[i];
 }
 let points44 = 0;
 for (let i = 3; i < 4; i++) {
                            points44 += points[i];
 }
 let points55 = 0;
 for (let i = 4; i < 5; i++) {
                            points55 += points[i];
 }
 
 // Get only the highest 5 points;
 
    console.log ("Points 1 is:", points11)
    console.log ("Points 2 is:", points22)
    console.log ("Points 3 is:", points33)
    console.log ("Points 4 is:", points44)
    console.log ("Points 5 is:", points55)

let toInsert = {
 "points1":        points11,
 "points2":        points22,
 "points3":        points33,
 "points4":        points44,
 "points5":        points55
};
    wixData.save("POINTS-PERSONAL", toInsert )
})

 .then((item) => {

I misunderstood what you want, I thought that a given player might have multiple entries in the database, in this case, you can ignore the first loop.

wixData.query('POINTS').eq('player','Ahmad').find().then((result) => {
    const player = result.items[0] || null;
    
    if (player) {
        const points = [];
        for (let i = 1; i <=6; i++) {
            points.push(player[`points${i}`]);
        }
        
        points.sort((a, b) => {
            return b - a;
        })
        
        const final_points = points.slice(0, 5);
    }
})

Now you have the highest 5 points out of 6 for the player “Ahmad” stored in an array and sorted descendingly, how you save them is completely up to you.