Site logo

Sort objects by geometric bounds function

Function by John Hawkinson. The source is here.

for (var p = 0; p < pages.length; p++) {
	page = pages[p];
	textFrames = page.textFrames.everyItem().getElements().sort(byYX);
}

function byYX(a, b) {
    var
        aY = a.geometricBounds[0],
        bY = b.geometricBounds[0],
        aX = a.geometricBounds[1],
        bX = b.geometricBounds[1],
        dy = aY-bY,
        dx = aX-bX;
  
    return dy?dy:dx;
}

Here is another approach by Marc Autret. (The original discussion is here).