Sort order of multi-reference field

I’m looking for info on how items stored in a multi-reference field are sorted, and whether they can be reliably saved and then accessed in a specific order?

Here’s the scenario:

  • I have a Collection of ingredients (called Ingredients).
  • Most ingredients are “raw” ingredients, but some are “made” ingredients, as in, a recipe may call for an ingredient which is itself made up of other sub-ingredients, e.g. “300g aioli (see made ingredient instructions for how to make the aioli)”
  • In my Ingredients collection, I added a multi-reference field called “subIngredients” which is meant to reference all the other ingredients that make up that “made” ingredient. Most ingredients won’t have this populated, but some do.
  • In addition (and here’s the tricky part), I’d like to record details about each of those sub-ingredients, mainly the specific sub-ingredient amounts that are required in a single batch of the “made” ingredient.
  • To do this, I added a separate field to my Ingredients collection called “subIngredientAmounts”, which is of type Array.

On my site, a user can create a new “Made” ingredient, and then add sub-ingredients to that ingredient, saving them in the multi-reference field of the collection.

Whenever a new sub-ingredient is added, I’m also adding the specified amount of that sub-ingredient in the subIngredientAmounts array. The order of the values in the array matters, because they are supposed to correspond to the order of elements in the multi-reference field.

However, I’m seeing some strange behavior when I add items to the subIngredients multi-reference field, and then try to access that data later. It seems as though the order in which I add subIngredients isn’t always maintained, which causes an issue because the order of the subIngredientAmounts in the Array field must correspond reliably to the order of the subIngredients listed in the multi-reference field.

Here’s an illustration of what I’m talking about:
First I create a new “Made” ingredient, with 3 sub-ingredients in the below order.

In my Ingredients database, these 3 sub-ingredients appear to be saved in the correct order:

Later, I go back to the Spicy aioli ingredient, and add a new sub-ingredient called “Avocado oil.” After adding this sub-ingredient, the sort of my sub-ingredients appears to have changed (and now the amounts are out of sync). It’s not alphabetical, but Achiote paste and Avocado oil have both moved to the top of the list…

And when looking in the Ingredients database, the sort order of the sub-ingredients appears to have changed (matching what I’m seeing as a user on the site).

Any ideas?

Update:
I think I’ve narrowed this down to what may be a difference between wixData.insertReference and wixData.replaceReferences…

In fact, my code seems to be working now with a small adjustment, but I’m still curious to understand why.

Previously, my code was using wixData.insertReference when adding a sub-ingredient for the first time, and wixData.replaceReferences when adding a sub-ingredient to an ingredient that already had sub-ingredients.

An added layer of complexity here was having to pull the current sub-ingredients down, get an array of their _id values, and then use push() to add the _id of the new subIngredient I wanted to add:

Turns out I didn’t need to make that distinction to begin with, as the following version seems to work and solve the original issue I posted about:

If anyone has any insight as to why these two approaches would lead to different results, would love to hear it!