Get group members in sorted order in directed graph

Trying to get a list of immediate members of a group so I can set the last node or group inside the parent to selected when parent group is expanded. However, it looks like the parts are not in any order. In my model the group has 2 sub-groups and the 2nd group is the first element of groupMembers. One more point, in our model the groups are not connected to each other or nodes only nodes inside groups are connected. How do you suggest ordering the parts so I can get the last member? Here is my code so far:

// get last member of group
const groupParts = group.memberParts;
const groupMembers = [];
groupParts.each((part) => {
  if (part instanceof go.Node || part instanceof go.Group) {

What do you mean by “last” member node? What kinds of graphs can be in a group?

It’s a directed graph. Groups are only place holder in order to group one or more nodes and nodes outside group are connected to nodes inside group. similar to this model:
const nodes = [
{ id: ‘1’, name: ‘A’ },
{ id: ‘2’, name: ‘B’ },
{ id: ‘g1’, name: ‘G1’, isGroup: true },
{ id: ‘g2’, name: ‘G2’, isGroup: true, group: ‘g1’ },
{ id: ‘5’, name: ‘E’, group: ‘g2’ },
{ id: ‘3’, name: ‘C’, group: ‘g1’ },
{ id: ‘4’, name: ‘D’, group: ‘g1’ }
const edges = [
{ from: ‘1’, to: ‘2’ },
{ from: ‘2’, to: ‘3’ },
{ from: ‘3’, to: ‘4’ },
{ from: ‘4’, to: ‘5’ }

Directed graphs can have cycles, which is why I was asking about how you determine the “last” node.

Even if you don’t have any cycles, there might be more than one node that is “last”.

But I guess you must have figured that out already. OK, if you already know the node(s) that need to be selected, you just need to implement a Group.subGraphExpandedChanged event handler to check whether the argument has Group.isSubGraphExpanded true, and if so, to set Part.isSelected on those node(s). Group | GoJS API