OpenMaya vertex order by connections

Trying to work on a script to change the vertex order of a selection returned by Maya. Currently it returns it selections based there vertexID’s in ascending order.

what i need is to return things in order according to the how the verts are connected to each other in a loop.

the code i got there works perfect for open loops, but for closed ones it messes up a little.

on open loops i get this result which is perfect.

but in the case here with a closed loop it messes up.

as you can see tries to go off in both directions from the start point.
when i need it to go [0, 5, 1, 6, 3, 4, 2, 8] or [0, 8 ,2, 4, 3, 6 ,1, 5]


class SelVerts(object):
    def __init__(self):
        sel = om.MSelectionList()
        om.MGlobal.getActiveSelectionList(sel)
        self.dag = om.MDagPath()
        self.component = om.MObject()
        sel.getDagPath(0, self.dag, self.component)

        self.ItrVert = om.MItMeshVertex(self.dag, self.component)

    def getVertIds(self):
        '''Get vertId's of selection'''
        vertIds = []
        self.ItrVert.reset()
        while not self.ItrVert.isDone():
            vertIds.append(self.ItrVert.index())
            self.ItrVert.next()
        return vertIds

    def numConnected(self, connected, vertIds):
        '''gets the numbers of connected verts from vertids that are in connected'''
        output = []
        for i in connected:
            if i in vertIds:
                output.append(i)
        return len(output)

    def getStart(self):
        '''finds the starting point of a open edge loop'''
        connected = om.MIntArray()
        vertIds = self.getVertIds()
        start = []
        self.ItrVert.reset()
        while not self.ItrVert.isDone():
            self.ItrVert.getConnectedVertices(connected)
            if self.numConnected(connected, vertIds) == 2:
                start.append(self.ItrVert.index())
            else:
                start = [self.ItrVert.index(), None]
            self.ItrVert.next()
        return start[0]

    def orderConnected(self):
        util = om.MScriptUtil()
        util.createFromInt(0)
        pInt = util.asIntPtr()
        connected = om.MIntArray()
        vertIds = self.getVertIds()

        mainList = []

        self.ItrVert.reset()
        self.ItrVert.setIndex(self.getStart(), pInt)
        self.ItrVert.getConnectedVertices(connected)

        mainList.append(self.ItrVert.index())
        vertIds.pop(vertIds.index(self.ItrVert.index()))

        while len(vertIds) > 0:
            self.ItrVert.getConnectedVertices(connected)
            for i in connected:
                if i in vertIds:
                    mainList.append(i)
                    vertIds.pop(vertIds.index(i))
                    self.ItrVert.setIndex(i, pInt)
        return mainList

fixed was just a simple mistake with me using a for loop at the end to check the connected verts against my vertIds list. It didnt know how to deal with the start point since it returned to connections that are in the vertIds list.
Was able to fixing it uses intersections and sets like so.


        while len(vertIds) > 0:
            self.ItrVert.getConnectedVertices(connected)
            inter = list(set(connected).intersection(vertIds))
            if len(inter) > 0:
                mainList.append(inter[0])
                vertIds.pop(vertIds.index(inter[0]))
                self.ItrVert.setIndex(inter[0], pInt)
        return mainList

am still open to anyone with ideas with ways to optimize this, or more efficient approaches to the same problem.