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

**URL:** <https://forum.kirupa.com/t/arrays-grids-lists-this-is-how-to-incrementally-move-items-up-and-down/210636>\
**Category:** Uncategorized\
**Created:** [December 20, 2006, 4:37pm UTC](https://forum.kirupa.com/t/arrays-grids-lists-this-is-how-to-incrementally-move-items-up-and-down/210636 "2006-12-20T16:37:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yawnnn](https://avatars.discourse-cdn.com/v4/letter/y/9d8465/32.png) [@Yawnnn](https://forum.kirupa.com/u/Yawnnn)\
**Post date:** [December 20, 2006, 4:37pm UTC](https://forum.kirupa.com/t/arrays-grids-lists-this-is-how-to-incrementally-move-items-up-and-down/210636/1 "2006-12-20T16:37:49Z")

</div>

Every now and then I learn something trivial, yet still new. 🙂 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. 🙂
