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

Discussion in 'Javascript' started by Tuxedo, Aug 31, 2008.

  1. Tuxedo

    Tuxedo Guest

    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!
     
    Tuxedo, Aug 31, 2008
    #1
    1. Advertising

  2. Tuxedo

    RobG Guest

    On Sep 1, 7:33 am, Tuxedo <> wrote:
    > 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.


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


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


    > 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
     
    RobG, Sep 1, 2008
    #2
    1. Advertising

  3. Tuxedo

    Tuxedo Guest

    RobG wrote:

    > On Sep 1, 7:33 am, Tuxedo <> wrote:


    [...]

    > 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
     
    Tuxedo, Sep 1, 2008
    #3
  4. In comp.lang.javascript message <g9f2o0$vn5$02$-online.com>,
    Sun, 31 Aug 2008 23:33:52, Tuxedo <> 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.

    --
    (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.
     
    Dr J R Stockton, Sep 1, 2008
    #4
  5. Tuxedo

    Tuxedo Guest

    Dr J R Stockton wrote:

    > In comp.lang.javascript message <g9f2o0$vn5$02$-online.com>,
    > Sun, 31 Aug 2008 23:33:52, Tuxedo <> 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 :)
     
    Tuxedo, Sep 1, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AtomicBob
    Replies:
    14
    Views:
    951
    Toby Inkster
    May 2, 2006
  2. nbigaouette

    Z-Ordering (Morton ordering) question

    nbigaouette, Nov 5, 2009, in forum: C Programming
    Replies:
    2
    Views:
    2,273
  3. Mike Owen

    Allowing entry of a Carriage Return during data entry

    Mike Owen, Jul 27, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    765
    Alessandro Zifiglio
    Jul 27, 2006
  4. Noozer
    Replies:
    2
    Views:
    289
    Dr John Stockton
    Aug 1, 2005
  5. David
    Replies:
    5
    Views:
    102
    Michael Winter
    Nov 8, 2006
Loading...

Share This Page