SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    815
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    sort array of objects.....

    how would I sort this array so the member with the highest points prints first?


    Code:
    
    	var members = [{
    		name: "stephen gupta",
    		points: 52
    	},{
    		name: "laura kowalski",
    		points: 67
    	},{
    		name: "John Rodriguez",
    		points: 33
    	}];
    	

    how would I sort this array so it prints the name with the most points first?

    for testing am printing contents thus:

    Code:
    for (var i = 0; i < members.length; i++) {
    	     $('#theMembers').append('name: '   + members[i].name +
    				' -- points: '  + members[i].points  + '<br>' );
    	}
      	

    but how do I sort it so the member with the most points prints first?
    I know array has .sort() method, but how does it work for a more complex array like this one?
    (is this an array of objects or a three-dimensional array? ;-) is it the same thing?


    thank you...

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by maya90 View Post
    how do I sort it so the member with the most points prints first?
    I know array has .sort() method, but how does it work for a more complex array like this one?
    What you can do with that is to supple the sort method with a function that controls how the sorting occurs.
    https://developer.mozilla.org/en-US/...cts/Array/sort

    Code javascript:
    members.sort(function (a, b) {
        return (a.points > b.points) ? 1 : -1;
    });

    You'll also need to use an array index in the for loop to access the array items in the members array.

    Quote Originally Posted by maya90 View Post
    (is this an array of objects or a three-dimensional array? ;-) is it the same thing?
    It's an array of objects.

    A three-dimensional array would be where each array item contains another array too.

    Code javascript:
    var three_d = [
        [
            ['0,0,0', '0,0,1', '0,0,2'],
            ['0,1,0', '0,1,1', '0,1,2'],
            ['0,2,0', '0,2,1', '0,2,2']
        ],
        [
            ['1,0,0', '1,0,1', '1,0,2'],
            ['1,1,0', '1,1,1', '1,1,2'],
            ['1,2,0', '1,2,1', '1,2,2']
        ]
    ];
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    815
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thank you...

    but I don't see here an example with an array that has the same structure as my array... these kinds of arrays can have a million different structures.. they're like JSON objects... it drives me nuts...

    everyplace where I look this up I see

    Code:
    members.sort(function (a, b) {.... }
    isn't this a comparison fn?

    if I want to sort by points how do I compare just two values? what if I have eight elements in the array?

    there's no simple way to say print the object with the highest no. of points first?

    this is for a test, there are 6 questions, this is the only one I can't do, I have been working on it for hours.. I have never worked with this kind of array and I find this really hard...

    this is the test:


    Code:
    var members = [{
    		name: "stephen gupta",
    		points: 52
    	},{
    		name: "laura kowalski",
    		points: 67
    	},{
    		name: "John Rodriguez",
    		points: 33
    	}];
    
    1) write a function that sorts the array by "points"
    2) write a function that creates an li elemnt for every item in the array, ex:

    Code:
    <li>name: stephen gupta<br>points: 52</li>
    I have been trying to do this all day, and I can't.... have found lots of stuff on stackoverflow, but nothing is exactly what I want...


    thank you...

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by maya90 View Post
    there's no simple way to say print the object with the highest no. of points first?
    There is, and that is to sort the contents so that it is ordered with the highests number of points being first, as according to the example that I gave.
    Here is some sample code that demonstrates it in action, too.
    http://jsfiddle.net/pmw57/gKGbY/
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    815
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    your fiddle works!! :-)

    thank you..

    (sorry, I find the code for this confusing...)

    so how about if I want to sort by points in reverse order?

    thank you...

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by maya90 View Post
    so how about if I want to sort by points in reverse order?
    You would have a separate function to sort them in reverse.

    Code javascript:
    function comparePoints(a, b) {
        return (a.points > b.points) ? 1 : -1;
    }
    function comparePointsReverse(a, b) {
        return (a.points < b.points) ? 1 : -1;
    }
     
    members.sort(comparePoints); // points in increasing order
    members.sort(comparePointsReverse); // points in decreasing order
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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