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 12-24-2011, 06:01 AM   PM User | #1
acromein
New to the CF scene

 
Join Date: Dec 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
acromein is an unknown quantity at this point
Sorting Objects (just for my understanding)

Hello,
I have an Object, containing an object, containing an object....etc.
So, it's an Object of objects. Not an Array of objects.
And now I want to sort this Object of objects.
Is there a "native" way in JavaScript to sort an object?
Or do I understand it correctly, that I first have to convert the Object to an Array, and than perform an array.sort ?

This question is just for my understanding of JavaScript, so I don't go into the wrong direction.
Tnx in advance for any explanation.
acromein is offline   Reply With Quote
Old 12-24-2011, 07:25 AM   PM User | #2
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,530
Thanks: 15
Thanked 128 Times in 121 Posts
chump2877 is on a distinguished road
Please see the following links:

http://www.w3schools.com/jsref/jsref_sort.asp
http://boxysystems.com/index.php/imp...in-javascript/

Array.sort accepts a callback function that you can use to sort the values in an array...If the values in the array are objects, then you can reference the object properties in your callback function -- and sort by object properties.

So, if you want to use the built in Array.sort feature, you'll have to convert one or more of your objects into arrays.

Of course, I don't see why you couldn't write your own sorting function for objects, and perhaps add the function to the Object prototype.
__________________
Regards, R.J.

---------------------------------------------------------

Help spread the word! Like my YouTube-to-Mp3 Web Conversion Software on Facebook !! :)
chump2877 is offline   Reply With Quote
Reply

Bookmarks

Tags
array, javascript, object, sort

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 03:46 PM.


Advertisement
Log in to turn off these ads.