const bookListsQuery = await modules.firestore.collection('books')
.get();
const sortedObj = Object.values(bookListsQuery.docs).sort(function(a, b){
console.log('a %s b %s', a.data().order, b.data().order)
return Number(a.data().order) > Number(b.data().order);
});
sortedObj.forEach(function(doc){
console.log(doc.data())
});
other way
object use map to array, then it sorted.
const bookListsQuery = await modules.firestore.collection('books')
.get();
const sortedArr = bookListsQuery.docs.map(function (doc) { // 轉換成array
return doc.data()
});
sortedArr.sort(function compare(a, b) {
return a.order > b.order; // 升 小->大
});
sortedArr.forEach(function(data){
console.log(data.data())
})
==========
Sorting multiple object properties with JavaScript
https://bithacker.dev/javascript-object-multi-property-sort
let students = [{
firstName: 'John',
lastName: 'Appletree',
grade: 12
},{
firstName: 'Mighty',
lastName: 'Peachtree',
grade: 10
},{
firstName: 'Kim',
lastName: 'Appletree',
grade: 11
},{
firstName: 'Shooter',
lastName: 'Appletree',
grade: 12
}];
let sortBy = [{
prop:'grade',
direction: -1
},{
prop:'lastName',
direction: 1
}];
array.sort(function(a,b){
let i = 0, result = 0;
while(i < sortBy.length && result === 0) {
result = sortBy[i].direction*(a[ sortBy[i].prop ].toString() < b[ sortBy[i].prop ].toString() ? -1 : (a[ sortBy[i].prop ].toString() > b[ sortBy[i].prop ].toString() ? 1 : 0));
i++;
}
return result;
})