code: def _sort_states(mapper, states):
      pending = set(states)
      persistent = {s for s in pending if s.key is not None}
      pending.difference_update(persistent)
  
      try:
          persistent_sorted = sorted(
              persistent, key=mapper._persistent_sortkey_fn
          )
      except TypeError as err:
          raise sa_exc.InvalidRequestError(
              "Could not sort objects by primary key; primary key "
              "values must be sortable in Python (was: %s)" % err
          ) from err
      return (
          sorted(pending, key=operator.attrgetter("insert_order"))
          + persistent_sorted
      )
  
imports: from __future__ import annotations
  from itertools import chain
  from itertools import groupby
  from itertools import zip_longest
  import operator
  from .None import attributes
  from .None import exc
  from .None import loading
  from .None import sync
  from .base import state_str
  from ..None import exc
  from ..None import future
  from ..None import sql
  from ..None import util
  from ..engine import cursor
  from ..sql import operators
  from ..sql.elements import BooleanClauseList
  from ..sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL
label_desc: Use image node_id 20 for calling a global function with example usage: _sort_states(mapper, states) and returns: unknown