How do you reorder an entity reference revisions field during hook_node_presave? I am trying to reorder some paragraphs based on a text field within them on node save so the author does not need to maintain the alphabetical order manually, but when I save the node all values are lost. I tried matching the array from $node->get('field_paragraphs')->getValue(), but that does not seem to work.

function mymodule_node_presave(NodeInterface $node) {
  if ($node-&gt;bundle() == 'page' &amp;&amp; $node&gt;get('field_paragraphs')-&gt;count() &gt; 1) {
    $values = $node-&gt;get('field_paragraphs')-&gt;getValue();
    $entities = $node-&gt;get('field_paragraphs')-&gt;referencedEntities();
    uasort($entities, function ($a, $b) {
      return Unicode::strcasecmp($a-&gt;get('field_text')-&gt;value, $b-&gt;get('field_text')-&gt;value);
    });

    $new_values = [];
    foreach ($entities as &amp;$entity) {
    $id = $entity-&gt;id();
    $new_value = [
      'target_id' =&gt; $id,
      'target_revison_id' =&gt; $entity-&gt;getRevisionId(),
    ];
    $index = array_search($id, array_column($values, 'target_id'));
    if (FALSE !== $index &amp;&amp; isset($values[$index]['subform'])) {
      $new_value['subform'] = $values[$index]['subform'];
    }
    $new_values[] = $new_value;
  }

  $node-&gt;set('field_paragraphs', $new_values);
}
You don’t need to re-create the array of target IDs by creating all that $new_values stuff.

Just set the entities like this:

$node-&gt;set('field_paragraphs', $entities);