How can I sort images by size in javascript or jquery

How can I sort images by size in JavaScript or jQuery. My code is as follows:

var imgsrc = ''; if (document.images.length < 1) { alert('No images to open'); return; } for (var i = 0; i < window.parent.document.images.length; i++) { imgsrc + = '\n'; } if (imgsrc != '') { var newwin = window.open('', ''); newwin.document.open(); newwin.document.write('\n' + imgsrc + ''); newwin.document.close(); } else { alert('No images!') }​ 

Please help me. Thank you in advance.

+4
source share
2 answers

See here for image sizes. You can sort the array, for example this ..sort () has an overload that performs the sort function. This is where you would compare the sizes.

+2
source

If you are using jquery:

 var images = $('img'); if ( images.length ) { // images were found images.sort( function (img1, img2) { return img1.width - img2.width } ); } 

fiddle works here

0
source

All Articles