Skip to main content
dublove
Legend
April 22, 2026
Answered

Can I sort objects in a group without ungrouping them?

  • April 22, 2026
  • 2 replies
  • 68 views

For example, I’ve selected a combination of an image and a text box.
I want to move the text box to the end of the selection without ungrouping the objects.


Below is the code that works when objects are not grouped.
Is there a way to perform operations on objects within a group?

// Sort the text box   Move the text box to the end
sel.sort(function (a, b) {
    var a;
    var b;
    if (“TextFrame” == a.constructor.name) {
        return 1;
    } else if (“TextFrame” == b.constructor.name) {
        return -1;
    }
});

 

    Correct answer rob day

    Get the group’s page items as an array via allPageItems (not pageItems)

     

    var s = app.activeDocument.selection;

    //if s is a group get its page items
    if (s.length == 1 && s[0].constructor.name == "Group") {
    s = s[0].allPageItems
    s[1].bringForward()
    }
    //if the first item is not a text frame reverse the array
    if (s.length == 2 && s[0].constructor.name == "TextFrame"){
    s[0].bringForward()
    }

     

     

    Before and after

     

     

    2 replies

    rob day
    Community Expert
    rob dayCommunity ExpertCorrect answer
    Community Expert
    April 23, 2026

    Get the group’s page items as an array via allPageItems (not pageItems)

     

    var s = app.activeDocument.selection;

    //if s is a group get its page items
    if (s.length == 1 && s[0].constructor.name == "Group") {
    s = s[0].allPageItems
    s[1].bringForward()
    }
    //if the first item is not a text frame reverse the array
    if (s.length == 2 && s[0].constructor.name == "TextFrame"){
    s[0].bringForward()
    }

     

     

    Before and after

     

     

    dublove
    dubloveAuthor
    Legend
    April 23, 2026

    @rob day 

    Try replacing it with an image. Why does “alert(s.length);” output 3 when it should be 2?
    Of course, in the example I provided, “alert(s.length);” should be 3, but the result is 4.

    It doesn't seem to work with images; it throws an error.

     

    rob day
    Community Expert
    Community Expert
    April 23, 2026

    Look in your Layers panel, there is a group plus 3 items:

     

     

    rob day
    Community Expert
    Community Expert
    April 22, 2026

    Hi ​@dublove , If the selection is only 2 items I don’t think you need the sort function—there is the array reverse() method. Maybe this?

     

    var s = app.activeDocument.selection;

    //if s is a group get its page items
    if (s.length == 1 && s[0].constructor.name == "Group") {
    s = s[0].allPageItems
    }
    //if the first item is not a text frame reverse the array
    if (s.length == 2 && s[0].constructor.name != "TextFrame"){

    s.reverse()
    }

    alert(s[0].constructor.name)

     

    dublove
    dubloveAuthor
    Legend
    April 23, 2026

    Hi ​@rob day

    I want to be able to adjust the parent-child relationships within a group without ungrouping it.

    These are group statuses, and generally speaking, you cannot control the objects within the group.