Sort on table calculated column

Hi,
I would like to order on the column I calculated named " ptot " ( rows[k][“ptot”] ).
I tried with the ’ descending (‘ptot’) ’ option on the query2 line but it doesn’t work.

Do you have any suggestions?

let query2 = wixData.query("CLASSIFICHE").contains('campionato', cmp).descending('ptot');

below is the code

thanks and excuse for my bad english
Claudio

let query2 = wixData.query("CLASSIFICHE").contains('campionato', cmp);
query2.find().then((results) => 
{
    $w("#table20").rows = results.items;
    $w("#table20").columns = 
    [{
 "id": "col0",
 "dataPath": "campionato",
 "label": "",
 "width": 40,
 "visible": true,
 "type": "string"
    },
    {
 "id": "col1",
 "dataPath": "squadre",
 "label": "Periodo",
 "width": 140,
 "visible": true,
 "type": "string"
    }]  
 
 let cols = $w("#table20").columns;
 cols.push
 ({
    "id": "col2",
    "dataPath": "ptot", 
    "label": "PT",
    "width": 40,
    "visible": true,
    "type": "number"
  });
  $w("#table20").columns = cols;
  let rows = $w("#table20").rows; 
 
   for (var k = 0; k < $w("#table20").rows.length; k++) 
   {
       rows[k]["ptot"] = $w("#table18").rows[k]['punticasaSum'] + $w("#table19").rows[k]['puntifuoriSum']; 
   }
   $w("#table20").rows = rows; 
})

if the “ptot” field is not in “CLASSIFICHE” collection, descending won’t work.
But you can use JS sort() method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

1 Like

Have you read the api reference for data query.
https://www.wix.com/corvid/reference/wix-data.html#query

Plus read here for the different options that you can do with the query itself, like your contains and descending option that you are using.
https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html