Skip to content

Instantly share code, notes, and snippets.

@nxvhm
Created January 12, 2022 14:03
Show Gist options
  • Save nxvhm/69d7b2aedf0e74920541b60e26fdb6a5 to your computer and use it in GitHub Desktop.
Save nxvhm/69d7b2aedf0e74920541b60e26fdb6a5 to your computer and use it in GitHub Desktop.
Order array of urls of images by their dimensions
let orderbyDimensions = images => {
let promises = [];
// console.log('to order by dimensions', images);
images.forEach(imgUrl => {
let img= new Image;
img.src = imgUrl;
let promise = new Promise( (resolve, reject) => {
img.onload=function(){
resolve({with: this.width, h:this.height, max: this.width*this.height, url: imgUrl});
};
});
promises.push(promise);
});
Promise.all(promises).then(values => {
values.sort((a, b) => {
if (a.max < b.max) return 1;
if (a.max > b.max) return -1;
});
console.log('resolved and sorted', values);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment