Fix categories ordering
XML input files may not lay out categories in the correct order, so we reorder them based on their index before calculating counter offset.
XML input files may not lay out categories in the correct order, so we reorder them based on their index before calculating counter offset.