Need help sorting table created dynamically

Hello, newbie to wix code and need help. I had loaded a table to maintain standings for a league I’m managing.
Using this instructions from this url:
Velo Tutorial: Calculating and Displaying Collection Data | Help Center | Wix.com I was able to summary the data per player.

I need help to sort by Category and Total Points after I have loaded the table, but I haven’t found a way. Please help. Here is the code I’m using:

$w(“#tblstandings”).columns = [
{
“id”: “col1”,
“dataPath”: “person_name”,
“label”: “Player”,
“type”: “string”
},
{
“id”: “col2”,
“dataPath”: “total_points”,
“label”: “Total Points”,
“visible”: true,
“type”: “number”
}
];
wixData.query(“WeeklyScores”)
.ne(“points_earned”,0)
.ascending(“category_name”,“person_name”)
.limit(1000) // include a limit if you have more than 50 items
.find()
.then( (result) => {
const persons = result.items.map(x => x.person_name)
.filter((obj, index, self) => index === self.indexOf(obj));

  const playersummary = persons.map(x => { 
    return { 
      person_name: x, 
      total_points: result.items.filter(obj => obj.person_name === x) 
              .map(z => z.points_earned) 
              .reduce((sum, current) => sum + current) 
    		}; 
	});    
$w("#tblstandings").rows = playersummary; 

});

Here is the end result that I’m looking for. Right now the data is sorted by player name in alphabetical order, but I needed by total points within the category.

Haven’t found a way to do this. Any ideas?

Hi dgamiami,
Do you have any trigger to run the sorting?

If you just want to sort the dataset when the page is ready, you only need to click your dataset icon on website editor.

Manager your dataset and scroll down you can set up dataset sorting and filter

The table is no linked to any dataset. It gets populated on the fly.

Hey,

It’s possible to sort by Total Points using the Javascript Array.sort function
For example, after you create the playersummary array, and before putting it on the table, try running the following:

playersummary.sort(function (a, b) {
return a.total_points - b.total_points;
});

Good Luck,
Itai

You are the best! Thank you for pointing me in the correct direction. I ended creating 3 different tables to accomplish what I needed, but it worked and I’m happy!.

Hi Itai, I have seen this thread as I am looking for help? I have no idea how to build this code.