How to sort the values obtained from monaca for mBaaS

Asked 1 years ago, Updated 1 years ago, 32 views

``
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?

Datastore View Screen

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.

javascript monaca

2022-09-30 16:01

1 Answers

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.
}


2022-09-30 16:01

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.