Arrays & Grids/Lists - This is how to incrementally move items up and down

Every now and then I learn something trivial, yet still new. :slight_smile: This is probably one of several ways to do this…

:ear: β€œ[COLOR=red]myData[/COLOR]” is an array that contains information displayed in β€œ[COLOR=red]myDataGrid[/COLOR]”. β€œ[COLOR=red]toBeMoved[/COLOR]” is a variable containing, um, the item to be moved. :ear:

[SIZE=1]btn_up.onRelease = function() { // this is a button to move items UP in the list[/SIZE]
[SIZE=1]whoseSelected = myDataGrid.selectedIndex; // gets the index # for the selected row/item[/SIZE]
[SIZE=1]if (whoseSelected > 0) { // makes sure you dont have the already top item selected[/SIZE]
[SIZE=1]toBeMoved = myData[myDataGrid.selectedIndex][/SIZE]
[SIZE=1]myData[myDataGrid.selectedIndex] = myData[myDataGrid.selectedIndex - 1];[/SIZE]
[SIZE=1]myData[myDataGrid.selectedIndex - 1] = toBeMoved;[/SIZE]
[SIZE=1]myDataGrid.dataProvider = myDataGrid.dataProvider; // refreshes the grid[/SIZE]
[SIZE=1]} else {[/SIZE]
[SIZE=1]trace (β€œItem is already at the top of the list - can’t move it any higher!”);[/SIZE]
[SIZE=1]};[/SIZE]
[SIZE=1]};[/SIZE]
[SIZE=1]};[/SIZE]

[SIZE=1]btn_down.onRelease = function() { // this is a button to move items DOWN in the list[/SIZE]
[SIZE=1]whoseSelected = myDataGrid.selectedIndex; // gets the index # for the selected row/item[/SIZE]
[SIZE=1]if (whoseSelected > < myDataGrid.length) { // makes sure you dont have the already last item selected[/SIZE]
[SIZE=1]toBeMoved = myData[myDataGrid.selectedIndex][/SIZE]
[SIZE=1]myData[myDataGrid.selectedIndex] = myData[myDataGrid.selectedIndex + 1];[/SIZE]
[SIZE=1]myData[myDataGrid.selectedIndex + 1] = toBeMoved;[/SIZE]
[SIZE=1]myDataGrid.dataProvider = myDataGrid.dataProvider; // refreshes the grid[/SIZE]
[SIZE=1]} else {[/SIZE]
[SIZE=1]trace (β€œItem is already at the bottom of the list - can’t move it any lower!”);[/SIZE]
[SIZE=1]};[/SIZE]
[SIZE=1]};[/SIZE]
[SIZE=1]};[/SIZE]

Do with it what you want. On the rare occasion that I figure something out I try to give back a bit since this site taught me quite a bit. :slight_smile: