Programmatically rearrange order of entity reference list on node My content type has a node reference field that allows users to enter an unlimited amount of nodes in. I need to programmatically change what comes first in this list.

Using the code below I'm able to obtain the data from the field.

$node = \Drupal::entityTypeManager()-&gt;getStorage('node')-&gt;load($function);
$focus = $node-&gt;field_focus_area-&gt;getValue();


And it produces:

array(2) {
  [0]=&gt;
  array(1) {
    ["target_id"]=&gt;
    string(2) "31"
  }
  [1]=&gt;
  array(1) {
    ["target_id"]=&gt;
    string(3) "161"
  }
}


In order to rearrange it I tried doing

$newOrder[0] = ['target_id'=&gt; 161]; 
$newOrder[1] = ['target_id'=&gt; 31];

$focus-&gt;field_focus_area-&gt;rekey($newOrder);


But it was then when I realized that rekey was a protected function so I could use it. How would I programmatically rearrange the deltas (if that's what you'd call it) of an entity reference field programmatically?
After getting and rearranging the field data you can set it again:

$node-&gt;field_focus_area-&gt;setValue($newOrder);