console.log(["iag12.png", "iq10.png", "img2.png", "img1.png"].sort(function(a, b) { var aw = a.match(/\D+/)[0], bw = b.match(/\D+/)[0], ad = a.match(/\d+/)[0], bd = b.match(/\d+/)[0]; return (aw > bw) - (aw < bw) || (ad-bd) - (bd-ad); }));