Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Nested Javascript Array

    Sorry folks, Im not sure that I am using the correct terminology here.

    What i would like to do is sort an array based on the [SortBy] value, however this array is nested. For instance:

    ar.[SortBy]"PartNumber"
    ar.[0].[PartNumber]"123"
    ar.[0].[PartName]"Widget1"
    ar.[1].[PartNumber]"345"
    ar.[1].[PartName]"Widget2"
    ar.[2].[PartNumber]"456"
    ar.[2].[PartName]"Widget3"
    ar.[3].[PartNumber]"567"
    ar.[3].[PartName]"Widget4"


    Thanks!

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Help us out by showing the contents of the nested array or how the array(s) are laid out.

  • #3
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    Help us out by showing the contents of the nested array or how the array(s) are laid out.
    this is it...
    ar.[SortBy]"PartNumber"
    ar.[0]
    .[PartNumber]"123"
    .[PartName]"Widget1"

    ar.[1]
    .[PartNumber]"345"
    .[PartName]"Widget2"
    ar.[2]
    .[PartNumber]"456"
    .[PartName]"Widget3"
    ar.[3]
    .[PartNumber]"567"
    .[PartName]"Widget4"

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts
    I don't recognize that as JS syntax and without the corresponding HTML to try to figure out what you are doing, I can be of little help.

    Good Luck!

  • #5
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    sorry.. im not trying to be cryptic. Maybe this screenshot of my debugger will help. Im pretty sure if you can visualize from the debugger image you will be able to easily tell me how to do it.


  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    So it *looks* like you are saying you have an array of objects.

    From JSON or maybe from an array of constructed objects?

    So that, for example, you can do
    Code:
        alert( ar[0].sPD ); // will show "PD10-000000209"
        alert( ar[0].sPDState ); // will show "Active"
    And so on?

    So if you wanted, for example, to sort that array based on the sPD member of each object, you could do:
    Code:
    ar.sort( function(o1,o2) { return o1.sPD < o2.sPD ? -1 : 1; } );
    That's not perfect: You really should return zero if o1.sPD == o2.sPD, but if that isn't too important to you, the above would work.

    If you really wanted to be able to do
    Code:
    ar.sortBySPD();
    or
    ar.sortBy("sPD");
    then you could add that as a method on a custom array prototype, implemented using the code I showed.

  • Users who have thanked Old Pedant for this post:

    salazar44 (06-20-2011)

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    Should have mentioned that you could also implement the sort function outside of the call to sort:

    Code:
    function bySPD( o1, o2 )
    {
        if ( o1.sPD == o2.sPD ) return 0;  // optional...not needed if values will never be equal
        return ( o1.sPD < o2.sPD ) ? -1 : 1; 
    }
    
    and then just do:
    
    ar.sort( bySPD );

  • Users who have thanked Old Pedant for this post:

    salazar44 (06-20-2011)

  • #8
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    So it *looks* like you are saying you have an array of objects.

    From JSON or maybe from an array of constructed objects?

    So that, for example, you can do
    Code:
        alert( ar[0].sPD ); // will show "PD10-000000209"
        alert( ar[0].sPDState ); // will show "Active"
    And so on?

    So if you wanted, for example, to sort that array based on the sPD member of each object, you could do:
    Code:
    ar.sort( function(o1,o2) { return o1.sPD < o2.sPD ? -1 : 1; } );
    That's not perfect: You really should return zero if o1.sPD == o2.sPD, but if that isn't too important to you, the above would work.

    If you really wanted to be able to do
    Code:
    ar.sortBySPD();
    or
    ar.sortBy("sPD");
    then you could add that as a method on a custom array prototype, implemented using the code I showed.
    Thanks for your help. This worked perfectly. I appreciate all the other options you provided as well.

    Now I have some more crazy things I gotta work through. For instance some of these objects within the array can not be sorted if they are a parent of other objects.

    bChildren = 'true' would indicate that the item is a parent and should not be part of the sort.

    Its a BOM (Bill of Materials) that Im trying to compose based on this array.

    Do you think it would be best if I composed/sorted the array at the c# level before I pass the array down to the client?
    Last edited by salazar44; 06-20-2011 at 06:34 PM.

  • #9
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Do I have to mark this thread as resolved or something like that?

  • #10
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Thumbs up

    Quote Originally Posted by salazar44 View Post
    Do I have to mark this thread as resolved or something like that?
    If you have gotten all the information you require ... yes.

  • #11
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    man... I dont see anything. Only thank user and add to reputation.

  • #12
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts
    "Thread Tools" near the middle right of the top page of this thread.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    [QUOTE=salazar44;1103601]
    Now I have some more crazy things I gotta work through. For instance some of these objects within the array can not be sorted if they are a parent of other objects.

    bChildren = 'true' would indicate that the item is a parent and should not be part of the sort.

    Its a BOM (Bill of Materials) that Im trying to compose based on this array.
    [/quote[


    If they are children, then they should not be members of the array of parent objects.

    Each parent should have a reference to its own array of children.

    Do you think it would be best if I composed/sorted the array at the c# level before I pass the array down to the client?
    Absolutely! In fact, if this info is coming from a database, then the sorting should almost surely be done in SQL, even before C# gets involved.

    This also applies to the consolidation question you posted in your other thread. It would be *TRIVIAL* to do that consolidation in SQL. And that almost surely should be done there, not in C# and not in JavaScript.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •