Sorting vertex indices.
Answer here https://blender.stackexchange.com/a/36619/15543 shows how to sort vertex indices using bmesh.
Answer here How to order a list of vertices based upon position? goes some way to show how to walk the edges for an order.
To be sure to be sure would walk the edges as shown above until the first vert is re-encountered (since its a loop) and use this for the order. Will leave it up to you to piece together the script from what is here
As demonstrated in @Leander's answer
Shortest Path
Using the script from What is the bmesh equivalent to bpy.ops.mesh.shortest_path_select()? could select edge as link between first and last index vert, temporarily remove it, find the shortest remaining path (the only path) from edges two verts. Reinstate the edge.
Face winding order
Instead for a closed loop like above, contend can fill with an ngon, use the ngon face verts as order (the winding order) re-sort the verts and remove the ngon face created.
import bpy
import bmesh
ob = bpy.context.object
assert ob.type == "MESH"
me = ob.data
bm = bmesh.from_edit_mesh(me)
f = bmesh.ops.contextual_create(
bm,
geom=bm.verts[:] + bm.edges[:],
)["faces"][0]
for i, v in enumerate(f.verts):
v.index = i
bm.verts.sort()
bm.faces.remove(f)
bmesh.update_edit_mesh(me)
Select a first vert and clockwise or counter
To select the zeroth vert, and ensure anticlockwise (from top view) indexing
Run in edit mode, select the vert you wish to have index 0. Assumes the shape lies in the XY plane. If the normal of the created face points down the winding is flipped.
Random order, vert 22 selected, script below run
import bpy
import bmesh
from mathutils import Vector
ob = bpy.context.object
assert ob.type == "MESH"
me = ob.data
bm = bmesh.from_edit_mesh(me)
v0 = bm.select_history.active
f = bmesh.ops.contextual_create(
bm,
geom=bm.verts[:] + bm.edges[:],
)["faces"][0]
fverts = f.verts[:]
if f.normal.dot((0, 0, 1)) < 0:
fverts.reverse()
for i, v in enumerate(fverts):
v.index = i
if v is v0:
iv0 = i
for v in fverts:
v.index = (v.index - iv0) % len(fverts)
bm.verts.sort()
bm.faces.remove(f)
bmesh.update_edit_mesh(me)