Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-17-2011, 10:09 PM   PM User | #1
salazar44
New to the CF scene

 
Join Date: Jun 2011
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
salazar44 is an unknown quantity at this point
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!
salazar44 is offline   Reply With Quote
Old 06-17-2011, 11:17 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
Help us out by showing the contents of the nested array or how the array(s) are laid out.
jmrker is offline   Reply With Quote
Old 06-17-2011, 11:22 PM   PM User | #3
salazar44
New to the CF scene

 
Join Date: Jun 2011
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
salazar44 is an unknown quantity at this point
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"
salazar44 is offline   Reply With Quote
Old 06-18-2011, 12:09 AM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
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!
jmrker is offline   Reply With Quote
Old 06-18-2011, 12:27 AM   PM User | #5
salazar44
New to the CF scene

 
Join Date: Jun 2011
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
salazar44 is an unknown quantity at this point
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.

salazar44 is offline   Reply With Quote
Old 06-18-2011, 05:40 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
salazar44 (06-20-2011)
Old 06-18-2011, 05:42 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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 );
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
salazar44 (06-20-2011)
Old 06-20-2011, 06:24 PM   PM User | #8
salazar44
New to the CF scene

 
Join Date: Jun 2011
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
salazar44 is an unknown quantity at this point
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..
salazar44 is offline   Reply With Quote
Old 06-20-2011, 11:23 PM   PM User | #9
salazar44
New to the CF scene

 
Join Date: Jun 2011
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
salazar44 is an unknown quantity at this point
Do I have to mark this thread as resolved or something like that?
salazar44 is offline   Reply With Quote
Old 06-21-2011, 12:16 AM   PM User | #10
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
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.
jmrker is offline   Reply With Quote
Old 06-21-2011, 12:19 AM   PM User | #11
salazar44
New to the CF scene

 
Join Date: Jun 2011
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
salazar44 is an unknown quantity at this point
man... I dont see anything. Only thank user and add to reputation.
salazar44 is offline   Reply With Quote
Old 06-21-2011, 12:43 AM   PM User | #12
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
"Thread Tools" near the middle right of the top page of this thread.
jmrker is offline   Reply With Quote
Old 06-21-2011, 02:36 AM   PM User | #13
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
[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.

Quote:
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, object array, sort, sorting

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:50 AM.


Advertisement
Log in to turn off these ads.