0

I'm using Ionic Native to read files in the user device, but I need to sort them by modification date.

The method to read the files returns Promise<Entry[]>. To find out the modification date, I need to call Entry's getMetadata method which has the following signature:

getMetadata(successCallback: MetadataCallback, errorCallback?: ErrorCallback): void;

Then in the success callback I have access to the Metadata object which has the modificationDate property I need for sorting.

I'd appreciate any help I can get.

brunobastosg
  • 1,380
  • 19
  • 31

1 Answers1

1

You'd start by promisifying the getMetadata method:

function getMetadataPromise(entry) {
    return new Promise((resolve, reject) => {
        entry.getMetadata(resolve, reject);
    });
}

Then you can use it in your loop and await all metadata, after which you can sort the array by it:

readEntries().then(entries =>
    Promise.all(entries.map(entry =>
        getMetadataPromise(entry).then(metadata => {
            entry.metadata = metadata;
            return entry;
        })
    ))
).then(entries =>
    entries.sort((a, b) => a.metadata.num - b.metadata.num) // or whatever
).then(sortedEntries => {
    console.log(JSON.stringify(sortedEntries));
}, console.error);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375