Repeater position change

I can figure out how to achieve this. Please help.

What needs to happen:

In a repeater, there are several items. These items are connected to a database with a field named ‘Position’. The repeater must sort the items by the position from low to high. So when I delete item 3 using remove() by clicking a delete button within the item, the items 4 and 5 in this example, but actually all items higher than 3 must decrease by 1. Because then everything is correct again. 1-2-3-4. Otherwise, it will be 1-2-4-5.

Thanks a lot!

If the position is a value in the collection then you’ll have to update the collection. If you can afford having 1-2-4-5 and only wants to show the item index in the repeater, then you remove and refresh the dataset and add something like:

$w.onReady(() => {
    $w('#repeater1').onItemReady($i, iData, index) => 
        $i('#text1').text = `${iData.title }/Position=${index + 1}`;
    })
})

Thanks for your response!

Let’s put it another way.
I have within my javascript a few items from a collection by using this code:

WixData.query("Collection")
    .eq("key", "value")
    .find()
    .then( (results) => {
    items = results.items;
    }

All the items that are filtered need to update their field ‘position’ by decreasing it by one. Do you know I can achieve that?

Thanks again!

WixData.query("Collection")
    .eq("key", "value")
    .find()
    .then( (results) => {
    items = results.items;
    items = items.map(e => {
	 if(e.position){e.position =- 1};
	return e;
    })
})