Enforce array ordering in HA?

I’m using Valetudo on my robot vacuum cleaner and I have integrated it with MQTT with HA. I have these radio buttons which I select and then hit start. The start button will trigger a script and uses the radio buttons numbers as an array that is sent over MQTT to valetudo.

type: vertical-stack
cards:
  - type: custom:auto-entities
    card:
      type: entities
      state_color: true
      title: Välj sektioner att rengöra
    filter:
      include:
        - group: group.vacuum_rooms
      exclude: []
    show_empty: true
    sort:
      method: state
      reverse: false
      numeric: false
  - type: custom:button-card
    tap_action:
      action: call-service
      service: script.vacuum_clean_segments
      confirmation: false
      service_data: {}
      target: {}
    lock:
      enabled: >-
        [[[return states['group.vacuum_rooms'].state !== 'on' ||
        states['vacuum.valetudo_decimallivelycockroach'].state !== 'docked']]]
      exemptions: []
    entity: script.vacuum_clean_segments
    name: Starta
    show_state: false
    show_icon: false

scripts.yaml:

vacuum_clean_segments:
  sequence:
  - service: script.turn_on
    target:
      entity_id: script.vacuum_clean_segments_message
    data:
      variables:
        segments: '{{expand("group.vacuum_rooms") | selectattr("state","eq","on")
          | map(attribute="attributes.room_id") | list | to_json}}'
  mode: single
  alias: Starta robotdammsugaren
  icon: mdi:arrow-right
vacuum_clean_segments_message:
  alias: vacuum_clean_segments_message
  sequence:
  - service: mqtt.publish
    data:
      topic: valetudo/DecimalLivelyCockroach/MapSegmentationCapability/clean/set
      payload: '{"segment_ids": {{segments}}}'
  mode: single

groups.yaml

vacuum_rooms:
  name: Vacuum Rooms
  entities:
    - input_boolean.vacuum_laundry
    - input_boolean.vacuum_bedroom_small
    - input_boolean.vacuum_bedroom_large
    - input_boolean.vacuum_bedroom_medium
    - input_boolean.vacuum_main_entrance
    - input_boolean.vacuum_hallway
    - input_boolean.vacuum_livingroom_tv
    - input_boolean.vacuum_livingroom_table
    - input_boolean.vacuum_dining_area
    - input_boolean.vacuum_kitchen

So, this is all working as expected. I can select indivual rooms and tell the robot to only clean those segements.

My problem is that I would like the robot to clean the selected rooms in a specific order. If all rooms are selected, I would like the robot to start with “vacuum_bedroom_large”, then “vacuum_bedroom_medium” and so on but instead, it always starts in the kitchen for example.

Any idea how I can define the order of the segments to clean?

Start with the sorted list then select or reject items in the raw list:

If you think you will need the sorted list in other places you can store it either in the options of an Input Select or a custom Jinja Macro so that you have a single place to update in case changes are needed.