Two points:
- doc.layers is a collection of all layers in document, regardless of the group it is in. This might not guarantee any order since they are "not in same list" (likely it's the order of layers-panel fully extracted). If you want to process layers in one group, you should use the .artLayers-collection
- I think Photoshop processes layers from "top to bottom", i.e. both "from top of screen to bottom" in layers-panel and "from topmost to bottom" in z-order. If you want the other way, start from bottom.