Move Array entry up or down in the Array

D

David

Is there an effecient way to move an array entry up or down in the array?

For example. Say you have an array of 5 entries. They are all strings by the
way.

myArray = ("entry0","entry1","entry2","entry3","entry4");

What is needed is a mechanism/function to move any of the array entries up
or down in the array. I have been trying by first splice(selectedEntry) out
of the Array, then a couple of slice() and then concat() them all back
together but this seems very clumsy.

Does anyone know of a more elegant way to accomplish this?

Regards,
David J.
 
V

VK

David said:
Is there an effecient way to move an array entry up or down in the array?

For example. Say you have an array of 5 entries. They are all strings by the
way.

myArray = ("entry0","entry1","entry2","entry3","entry4");

What is needed is a mechanism/function to move any of the array entries up
or down in the array. I have been trying by first splice(selectedEntry) out
of the Array, then a couple of slice() and then concat() them all back
together but this seems very clumsy.

Does anyone know of a more elegant way to accomplish this?

<script type="text/javascript">
var arr = [0,1,2,3];

var tmp = arr.splice(3,1);
arr.splice(2,0,tmp);

alert(arr); // 0,1,3,2
</script>

IMHO you are seeking for List functionality rather than for Array's
one. See for instance
<http://groups.google.com/group/comp.lang.javascript/msg/19ac11e0164d8ab4>
as well as the whole thread which contains a valuable criticism.
 
D

David

Is there an effecient way to move an array entry up or down in the array?
<script type="text/javascript">
var arr = [0,1,2,3];

var tmp = arr.splice(3,1);
arr.splice(2,0,tmp);

alert(arr); // 0,1,3,2
</script>

IMHO you are seeking for List functionality rather than for Array's
one. See for instance
<http://groups.google.com/group/comp.lang.javascript/msg/19ac11e0164d8ab4>
as well as the whole thread which contains a valuable criticism.

Thank you, that is a very effecient way to do it...

David J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top