Sorting query by included referenced field does not work

Please help, what i am doing wrong? Items displayed in tableTest are not sorted…

let query= wixData.query(“EventSelectedFields”)
.eq(“sk4UnifiedEvent”,currentEventID[“_id”])
.include(“selectedField”)
.ascending(“selectedField.cefName”)
.find()
.then( (res) => {
if (res.items.length > 0) {
let items = res.items;
$w(‘#tableTest’).rows = items;

}  **else**  { 

// handle case where no matching items found
console.log(“no items found”);
}
} )
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
console.log(errorMsg);
} );

Make sure that you are using the correct code and that you also include to import wix data and add a onReady call to your page as well.
https://www.wix.com/corvid/reference/wix-data.html#query

My query is working, i am getting results and they are displayed in tableTest. So it impossible that wix data is missing or onReady problem.
The problem is that my results are not sorted by referenced field included in .include.

@chana

Yes that is fine, it just happens that some new to Wix Coders do just use the code on the page and if it doesn’t state to add imports and onReady call etc, then some of them don’t add it and still expect their code to work perfectly, so I was simply double checking that you’ve got that already too and you have and you’ve simply just pasted up the query part.

Anyways, as for the include error, you need to change the ‘referencefield’ to the actual field in your dataset that you want to reference.

https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html

wixData.query("yourdatasetname")
  .eq("sk4UnifiedEvent", currentEventID ["_id"])   
  .include("selectedField")
  // Is this the field in your dataset that you want to use for referencing?
  .ascending("selectedField.cefName")
  // Is this the field in your dataset that you want to have ascending?

@givemeawhisky Get the exactly same situation. Would like sort on a related field…a refenced field…a included field?

Is this possible. Think is the same question?!

// Example:
// Database Table "Result" contain (here as pseudo-code: 
// [ e
//  { catRef <ReferenceField to Category>: A, ranking: 1, ...},
//  { catRef <ReferenceField to Category>: A, ranking: 2, ...},
//  { catRef <ReferenceField to Category>: A, ranking: 3, ...},
//  { catRef <ReferenceField to Category>: B, ranking: 1, ...},
//  { catRef <ReferenceField to Category>: B, ranking: 2, ...},
//  { catRef <ReferenceField to Category>: C, ranking: 1, ...},
//  { catRef <ReferenceField to Category>: C, ranking: 2, ...},
//  ...
// ]

// Database Table "Category" contain:
// [ e 
//  { _id: "A", priority: 2, name: "Alpha"}, 
//  { _id: "B", priority: 3, name: "Beta"}, 
//  { _id: "C", priority: 1, name: "Gamma"},  
//  ... 
// ]

// Now: would like make a query like:
let data = wixData.query("Result")
  .include("Category")   
  .ascending("catRef.priority", ranking)     
  .find()   
  .then( (res) => {
    return res.items
  }
  
// To get items in order like:
[ 
  { catRef <...>: C, ranking: 1, {_id: "C", priority: 1, name: "Gamma"},
  { catRef <...>: C, ranking: 2, {_id: "C", priority: 1, name: "Gamma"},
  { catRef <...>: A, ranking: 1, {_id: "A", priority: 2, name: "Alpha"},
  { catRef <...>: A, ranking: 2, {_id: "A", priority: 2, name: "Alpha"},
  { catRef <...>: A, ranking: 3, {_id: "A", priority: 2, name: "Alpha"},
  { catRef <...>: B, ranking: 1, {_id: "B", priority: 3, name: "Beta"},
  { catRef <...>: B, ranking: 2, {_id: "B", priority: 3, name: "Beta"},
]