Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » JavaScript

Simple Q: Re-ordering array by specifying first-entry

 
Jump to:  
 
Tuxedo
PostPosted: Sun Aug 31, 2008 7:33 pm    Post subject: Simple Q: Re-ordering array by specifying first-entry
       
I have an array in a particular order in a frame (mainframe) which I'd like
to reorder from an onload call of another frame, while from that frame, the
only thing I'd like to specify is which one named array item should be the
first.

In other words, if this is the original array:

var myArray = new Array('a1,'a2','a3','a4','a5')

I'd like to reorder it via a function by only specifying which one item I'd
like to have first. The function, in the same frame as the array, could be
named something lfor example:

function reorder_the_array(i){
swapping or reorder code goes here
}

So if from the other frame I call reorder_the_array() and pass the one item
I'd like to have first, for example, 'a3':
<body onload="top.mainframe.reorder_the_array('a3')";

What's a good way to change the array or make a copy of it and create a new
one without specifying all the array items from where the function is
called?

As mentioned, all I would like to specify in the call from the second frame
is which one item I would like to have first. So for example, by running
reorder_the_array('a3'), the reordered or new array would then become:

a3,a2,a1,a4,a5

If as above, that would simply be swapping positions of a1 and a3.
However, I may sometimes pass 'a1' as the item to be the first in which
case there doesn't need to be any chance. But this can easily be done by
preventing the swapping code from running by a simple if condition.

In fact, it doesn't make any difference which exact order the items are
juggled in the remaining part of the array, as long as:
1) The item passed through the function is placed first
2) The remaining array entries are all unique, so all original array items
exist and that it is the same length as the original.

I guess this is a pretty simple task, and there are probably many ways to
do it. However, I'm note very well-versed on this subject so any pointers
would be mostly appreciated.

Many thanks!
 

 
RobG
PostPosted: Mon Sep 01, 2008 1:27 am    Post subject: Re: Simple Q: Re-ordering array by specifying first-entry
       
On Sep 1, 7:33 am, Tuxedo <tux...@mailinator.com> wrote:
Quote:
I have an array in a particular order in a frame (mainframe) which I'd like
to reorder from an onload call of another frame, while from that frame, the
only thing I'd like to specify is which one named array item should be the
first.

In other words, if this is the original array:

var myArray = new Array('a1,'a2','a3','a4','a5')

I'd like to reorder it via a function by only specifying which one item I'd
like to have first.

How about:

Array.prototype.makeFirst = function(idx) {
this.unshift(this.splice(idx, 1));
return this;
}

alert( ['a1','a2','a3','a4','a5'].makeFirst(2) );

shows: 'a3','a1,'a2','a4','a5'


You may want to check that idx is < arr.length and deal with that,
otherwise you will get an empty array as the first element. Also
check idx isn't < 0, you may want to return undefined, do nothing,
throw an exception, whatever.

Pretty sure you need to modify Array.prototype in the called frame, I
don't use frames, please test that.


Quote:
The function, in the same frame as the array, could be
named something lfor example:

function reorder_the_array(i){
swapping or reorder code goes here
}

If you want it as a function:

function makeFirst(arr, idx) {
arr.unshift(arr.splice(idx, 1));
return arr;
}

or perhaps:

function makeFirst(arr, idx) {
return [].concat(arr.splice(idx, 1), arr);
}


In this case you need to pass it the array too unless you hard-code
some global variable that references the array (and that global
variable is in the right frame). Again you need to deal with idx >length or < 0.


Quote:
So if from the other frame I call reorder_the_array() and pass the one item
I'd like to have first, for example, 'a3':
body onload="top.mainframe.reorder_the_array('a3')";

What's a good way to change the array or make a copy of it and create a new
one without specifying all the array items from where the function is
called?

If you want to keep the original array unmodified, you need to copy it
first.


Quote:
As mentioned, all I would like to specify in the call from the second frame
is which one item I would like to have first. So for example, by running
reorder_the_array('a3'), the reordered or new array would then become:

a3,a2,a1,a4,a5

If as above, that would simply be swapping positions of a1 and a3.  
However, I may sometimes pass 'a1' as the item to be the first in which
case there doesn't need to be any chance. But this can easily be done by
preventing the swapping code from running by a simple if condition.

In fact, it doesn't make any difference which exact order the items are
juggled in the remaining part of the array, as long as:
1) The item passed through the function is placed first
2) The remaining array entries are all unique, so all original array items
exist and that it is the same length as the original.

The unique constraint wasn't there before, I guess you mean you want
the nominated element to be moved, not copied.


--
Rob
 

 
Tuxedo
PostPosted: Mon Sep 01, 2008 3:49 am    Post subject: Re: Simple Q: Re-ordering array by specifying first-entry
       
RobG wrote:

Quote:
On Sep 1, 7:33 am, Tuxedo <tux...@mailinator.com> wrote:

[...]

Quote:
The unique constraint wasn't there before, I guess you mean you want
the nominated element to be moved, not copied.


Yes exactly. I think I could have described it much better like that.

I guess it could also be done by pattern-matching:
1) identifying the position of the item that needs to be moved.
2) deleting that array entry.
3) insterting it as the first item of the array.

Step #1 would require searching for the position of a string match in an
array and returning its position for later deleting it. How can this be
done?

In any case, many thanks for the various examples, will test them today.

Tuxedo
 

 
Dr J R Stockton
PostPosted: Mon Sep 01, 2008 9:27 am    Post subject: Re: Simple Q: Re-ordering array by specifying first-entry
       
In comp.lang.javascript message <g9f2o0$vn5$02$1@news.t-online.com>,
Sun, 31 Aug 2008 23:33:52, Tuxedo <tuxedo@mailinator.com> posted:

Quote:
I have an array in a particular order in a frame (mainframe) which I'd like
to reorder from an onload call of another frame, while from that frame, the
only thing I'd like to specify is which one named array item should be the
first.

Consider

A = [6,7,8,9]
function X(x) { for (var J in A) if (A[J]==x) { A[J]=A[0] ; A[0]=x } }
X(8)

There is no need to avoid swapping the zeroth item, but it can be done :
function X(x) { J=A.length
while (--J) if (A[J]==x) { A[J]=A[0] ; A[0]=x } }

To preserve order, seek the element to be moved, use .splice(J,1) to
remove it, and join it to the front.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 

 
Tuxedo
PostPosted: Mon Sep 01, 2008 4:03 pm    Post subject: Re: Simple Q: Re-ordering array by specifying first-entry
       
Dr J R Stockton wrote:

Quote:
In comp.lang.javascript message <g9f2o0$vn5$02$1@news.t-online.com>,
Sun, 31 Aug 2008 23:33:52, Tuxedo <tuxedo@mailinator.com> posted:

I have an array in a particular order in a frame (mainframe) which I'd
like to reorder from an onload call of another frame, while from that
frame, the only thing I'd like to specify is which one named array item
should be the first.

Consider

A = [6,7,8,9]
function X(x) { for (var J in A) if (A[J]==x) { A[J]=A[0] ; A[0]=x } }
X(8)

There is no need to avoid swapping the zeroth item, but it can be done :
function X(x) { J=A.length
while (--J) if (A[J]==x) { A[J]=A[0] ; A[0]=x } }

To preserve order, seek the element to be moved, use .splice(J,1) to
remove it, and join it to the front.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.


Thanks for posting this and the previous example by RG. I tested them and
found they all work, and will keep all in an advanced JS folder as good
examples for future use. I realised, however, by spending a couple of hours
tinkering with it, that what needs to be done is actually much simpler and
something different altogether. So after thinking about what exactly I
need, for about 3 minutes, it only took about 10 minutes to write it
together. Totally and utterly simple stuff I wouldn't want to post here Smile
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

projekty domów parterowych Diety hvac in Chicago contractors organizacja wesel nowe projekty domów