我正在从Firebase存储中检索一些图像(仅URL)。
这很好
async loadStoragePictures(){
fb.storage.ref('images/products').listAll().then(snap => {
const imageUrlArray: any = []
snap.items.forEach(itemRef => {
itemRef.getDownloadURL().then(imgUrl => {
imageUrlArray.push(imgUrl)
})
})
store.commit('setPictures', imageUrlArray)
})
},
不幸的是,我想按上次更改日期对这些URL进行排序。
我在快速帮助中看到的方式.sort(by: { $0.name > $1.name })
(或 lastAction 的等效项)不起作用。
那我如何才能对snap.items
中的数据进行排序?
答案 0 :(得分:1)
在提交之前简单地添加一个排序:
async loadStoragePictures(){
fb.storage.ref('images/products').listAll().then(snap => {
const imageUrlArray: any = []
const myItems = snap.items.sort((a, b) => {
return a.name.localeCompare(b.name);
});
myItems.forEach(itemRef => {
itemRef.getDownloadURL().then(imgUrl => {
imageUrlArray.push(imgUrl)
})
})
store.commit('setPictures', imageUrlArray)
})
},