Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Move Array entry up or down in the Array

1 view
Skip to first unread message

David

unread,
Nov 8, 2006, 12:32:31 AM11/8/06
to
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.


VK

unread,
Nov 8, 2006, 3:47:37 AM11/8/06
to

<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.

Lich_Ray

unread,
Nov 8, 2006, 8:09:46 AM11/8/06
to
I have seen your List class on
<http://groups.google.com/group/comp.lang.javascript/msg/19ac11e0164d8ab4>

You $add() method is so cool !!

VK

unread,
Nov 8, 2006, 8:17:09 AM11/8/06
to

As indicated at the top of the linked post, the coolest (IMHO) part
with splice-zero was suggested by poster signing as "Ray".

David

unread,
Nov 8, 2006, 9:57:27 AM11/8/06
to
>> 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.


Michael Winter

unread,
Nov 8, 2006, 12:45:06 PM11/8/06
to
Lich_Ray wrote:
> I have seen [VK's] List class on

Despite the poor implementation?

Mike


Please quote when replying to posts in this group.

0 new messages