Getting sorted memberParts

I am finding out the hard way right now that after a layout, a call to memberParts doesn’t return the parts sorted.

I have this:

CompactGridLayout.prototype.doLayout = function(coll) {
	go.GridLayout.prototype.doLayout.call(this, coll);

	var diagram = this.diagram;
	if (diagram === null) return;

	diagram.startTransaction("CompactGridLayout");

	var group = this.group;
	console.log("Group " + getNodeText(group));

	//get the nodes, properly sorted //<===old comment from original dev
	var nodes = new Array();
	var memberPartsIterator = group.memberParts.iterator;
	while (memberPartsIterator.next()) {
		var part = memberPartsIterator.value;
		console.log("Member part " + getNodeText(part) + " at index:" + getIndex(part));
		if (part.isLayoutPositioned && part.visible) {  //only consider layout managed
			nodes.push(part);
		}
	}

then it uses that to perform some post layouting that is required is some complex cases. That worked before since the data came pre-sorted and the result of applying the comparer were identical.

Is there a built-in way to get the sorted parts? Or at least a way to get their sorted index order?

Thanks
Alain

Yes, the order of Group.memberParts is not supposed to be modified by any layout. In fact, either before or after a layout there might not be any one “proper” linear ordering of the Nodes and Links. Although you probably have an idea of what you would like for your app. So you should sort the collection (just of Nodes, I guess) in whatever order you like. Maybe you want to do something like this?

new go.List(aGroup.memberParts.filter(p => p instanceof go.Node)).sort((a, b) => a.location.y - b.location.y)

Which sorts the group’s member Nodes by their Y location.

By the way, instead of part.isLayoutPositioned && part.visible you can just call Part.canLayout: Part | GoJS API

since I wanted the result of the layout sorting, I just called sort with the comparer (just overhead):

	var nodes = new Array();
	var memberPartsIterator = group.memberParts.iterator;
	while (memberPartsIterator.next()) {
		var part = memberPartsIterator.value;
		console.log("Member part " + getNodeText(part) + " at index:" + getIndex(part));
		if (part.canLayout()) {  //only consider layout managed
			nodes.push(part);
		}
	}

	nodes.sort(this.comparer);