``
If you keep this code, the order of names will be different every time because you do not specify the order of names.
I'd like to see them in ascending order in the order of shop names. How should I write them?
The code to be ordered is listed in the official document, but I didn't know how to specify it, so I asked you a question.
Thank you for your cooperation.
I'd like to see them in ascending order in the order of shop names. How should I write them?
Monaca datastores seem to be sorted by the order
method.
app.js: I think you can sort by changing line 211 to the following.
ShopClass.order("name").fetchAll()
You can sort object arrays as follows:(Reference: Array.prototype.sort())
shopArray=[{name:'shopC'}, {name:'shopB'}, {name:'shopA'}];
var sortedShopArray=shopArray.sort(function(a,b){
if(a.name>b.name){
return1;
} else if(a.name===b.name){
return 0;
} else{
return-1;
}
})
console.log(sortedShopArray);//=> [{name:'shopA'}, {name:'shopB'}, {name:'shopC'}]
Therefore, I think it would be better to sort the list of shops first as below and then process it.
//Sort shop list
var sortedShopArray=shopArray.sort(function(a,b){
if(a.name>b.name){
return1;
} else if(a.name===b.name){
return 0;
} else{
return-1;
}
});
// View Shop List
for (vari=0;i<sortedShopArray.length;i++) {
varshop=sortedShopArray[i];
// The following is an abbreviation.
}
© 2024 OneMinuteCode. All rights reserved.