you are missing the alphabetical order of the items!
so maybe your issue is in the sort() method
if you want to copy the array, that would be the way, yes, but even then, as this is a multidmensional array you are copying the outer array but not the inner ones
you could do let entireInv = JSON.parse(JSON.stringify(curInv));
it can be considereded “hacky”, but it is the easiest way to copy a multidimensional array or object, otherwise you would need to copy each sub array separately, like for example let entireInv = curInv.map(x => [...x]) (but if you don’t how many levels you have it doesn’t work)
spread operator copy an array, but it copys only the highest level array, if it is a multidimensional array the way in my previous post is the only way without creating a complicated (and unnecessary, as there is that way already) algorithm