rearranged sorting function, added update of vertex id