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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,857
    Thanks
    22
    Thanked 157 Times in 148 Posts
    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 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪


  •  

    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
    •