How to change (re-order) the deltas programmatically in node_presave In a node, I have a multi-value field "field_MV" containing 3 items

I don't think it is important, but field_MV  is of type "Entity reference revisions" and the items are paragraphs of only one type.

Each paragraph (item) have a field "field_quantity" containing a number.

Here is my exemple:

$node-&gt;field_MV[0]-&gt;field_quantity-&gt;value=5 (paragraph at delta 0)
$node-&gt;field_MV[1]-&gt;field_quantity-&gt;value=3 (paragraph at delta 1)
$node-&gt;field_MV[2]-&gt;field_quantity-&gt;value=7 (paragraph at delta 2)


In hook_node_presave, I would like to loop in the paragraphs to change their delta according the the value of field_quantity (descending)

Here is the result I would like to have:

$node-&gt;field_MV[0]-&gt;field_quantity-&gt;value=7 (previously paragraph delta 2)
$node-&gt;field_MV[1]-&gt;field_quantity-&gt;value=5 (previously paragraph delta 0)
$node-&gt;field_MV[2]-&gt;field_quantity-&gt;value=3 (previously paragraph delta 1)


My question is not how to order (I can handle this), but how to change the delta with something like:

$node-&gt;field_MV[0]-&gt;set("delta",1);
$node-&gt;field_MV[0]-&gt;save();
You would want to set the field value to an array in the order you want them. Since you are doing this inside hook_node_presave, you don't have to bother saving the node. It will be done for you.

Something like this should work:

$values = $node-&gt;get('field_mv')-&gt;getValue();

... your sorting function ...

$node-&gt;set('field_mv', $values);