I'm trying to sort an array of objects with an attribute page. This is being done in a computed property with Vue.
To do this I'm using the following function, which first builds the objects:
sorted: function(){
var pages = this.selectedKKS['pages'];
var list;
try {
list = [];
Object.keys(pages).forEach(function(key){
console.log(key + " is the key")
var obj = {};
obj.title = key;
obj.page = pages[key]
list.push(obj)
});
}
catch(e){
console.log(e);
}
var sorted = list.sort(function(a, b){
console.log('a.page is ' + a.page + ' and b.page is ' + b.page);
return a.page > b.page;
});
return sorted;
}
Just to make sure that I'm actually comparing pages as I intend, here's the console log:
a.page is 84 and b.page is 28
App.vue?077f:353 a.page is 84 and b.page is 46
App.vue?077f:353 a.page is 28 and b.page is 46
App.vue?077f:353 a.page is 84 and b.page is 35
App.vue?077f:353 a.page is 46 and b.page is 35
App.vue?077f:353 a.page is 28 and b.page is 35
App.vue?077f:353 a.page is 84 and b.page is 14
App.vue?077f:353 a.page is 46 and b.page is 14
App.vue?077f:353 a.page is 35 and b.page is 14
App.vue?077f:353 a.page is 28 and b.page is 14
App.vue?077f:353 a.page is 84 and b.page is 5
App.vue?077f:353 a.page is 46 and b.page is 5
App.vue?077f:353 a.page is 84 and b.page is 8
App.vue?077f:353 a.page is 5 and b.page is 8
I'm looping over this computed property in my template, and since it's being sorted incorrectly it's giving me an undesirable result:
<f7-list>
<f7-list-item v-for="val in sorted" @click="openpdf(selectedKKS.url, val.page)">
<f7-col><span style="color: black">{{ val.title }}</span></f7-col>
<f7-col>{{ val.page }}</f7-col>
</f7-list-item>
</f7-list>
Any ideas as to what could be going wrong?

return a.page - b.page;pageproperties are strings. So you're doing string comparisons, for which the result is valid.