473,378 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Move Array entry up or down in the Array

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.
Nov 8 '06 #1
5 7948
VK

David wrote:
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.

Nov 8 '06 #2
I have seen your List class on
<http://groups.google.com/group/comp.lang.javascript/msg/19ac11e0164d8ab4>

You $add() method is so cool !!

Nov 8 '06 #3
VK

Lich_Ray wrote:
I have seen your List class on
<http://groups.google.com/group/comp.lang.javascript/msg/19ac11e0164d8ab4>

You $add() method is so cool !!
As indicated at the top of the linked post, the coolest (IMHO) part
with splice-zero was suggested by poster signing as "Ray".

Nov 8 '06 #4
>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.
Nov 8 '06 #5
Lich_Ray wrote:
I have seen [VK's] List class on
<http://groups.google.com/group/comp.lang.javascript/msg/19ac11e0164d8ab4>

You $add() method is so cool !!
Despite the poor implementation?

Mike
Please quote when replying to posts in this group.
Nov 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: sathya | last post by:
I was going through an Boyer-Moore-Horspool pattern match, I saw a array inside a array, like the below, skip ] = patlen - i - 1; The array is decleared as int skip; unsigned char *pat;
1
by: Samuel R. Neff | last post by:
Are there any differences between using Array.Length and Array.GetUpperBound(0) on a one-dimensional array? We have a team of developers and most people use Array.Length but one developer uses...
6
by: routeslip | last post by:
I'm refering to an entry in an array by it's string key, as in foo. Is there a way to get the numeric index of that array without iterating through the entire array? What I need to do is find...
1
by: beachboy | last post by:
How I can Move up or down XmlElement? Please advise. Thanks in advanced.
2
by: beachboy | last post by:
How I can Move up or down XmlElement? Please advise. Thanks in advanced.
7
by: Adam Honek | last post by:
Is there a direct way to remove one entry from a one dimensional array? I keep looking but either my eyes are funny or it isn't there. Must we really create a temp array and copy all but 1 of...
0
by: K B | last post by:
Hi again, I have a treeview, I would like to select a node, then click on an up or down button and move the node accordingly, updating the xmlDocument each time. For example, if I select a 2nd...
2
by: james | last post by:
Hi Guys, Would anyone mind telling me the difference between Array.Clone and Array.CopyTo? I was asked this during an interview and got partial credit for saying they were basically the same...
4
by: mattmao | last post by:
Hello everyone: Recently we were given this example: int array={0,1,2,3,4,5,6,7,8,9}; Of course if you printf("%d", array); the output would be 2. But our tutor raised such a confusing...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.