SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member stix_fs's Avatar
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Sorting an array of scores

    For example if I have this function name:

    function sortScores(myscores){

    I would like to take an array of score records (each containing a score and a page number) and return a sorted version where the records are arranged in descending order of score.

    so if I have this example call:

    sortScores( [ { score:0, Number:0 }, { score:2, Number:1 }, { score:1, NUmber:2 } ] )


    I would get the example result:

    [ { score:2, Number:1 }, { score:1, Number:2 }, { score:0, Number:0 } ]

    Where the results have been sorts in descending order of score.

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

    I've looked at this example, but it only deals with one one value -->

    http://www.javascriptkit.com/javatutors/arraysort.shtml

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function sortScores(myscores){
    	return myscores.sort(function(a, b){
    		var n= a.score-b.score;
    		if(n=== 0){
    			return a.Number-b.Number;
    		}
    		return n;
    	});
    }

  3. #3
    SitePoint Member stix_fs's Avatar
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    Code:
    function sortScores(myscores){
    	return myscores.sort(function(a, b){
    		var n= a.score-b.score;
    		if(n=== 0){
    			return a.Number-b.Number;
    		}
    		return n;
    	});
    }

    Okay, so how would I call the function, I tried using an alert but it just returned

    [object Object],[object Object],[object Object]

  4. #4
    SitePoint Member stix_fs's Avatar
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stix_fs View Post
    For example if I have this function name:

    function sortScores(myscores){

    I would like to take an array of score records (each containing a score and a page number) and return a sorted version where the records are arranged in descending order of score.

    so if I have this example call:

    sortScores( [ { score:0, Number:0 }, { score:2, Number:1 }, { score:1, NUmber:2 } ] )


    I would get the example result:

    [ { score:2, Number:1 }, { score:1, Number:2 }, { score:0, Number:0 } ]

    Where the results have been sorts in descending order of score.

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

    I've looked at this example, but it only deals with one one value -->

    http://www.javascriptkit.com/javatutors/arraysort.shtml

    I think Number was the wrong name to use so instead it's this:

    call this function:
    sortScores( [ { score:0, Num:0 }, { score:2, Num:1 }, { score:1, Num:2 } ] )


    I would get the example result:

    [ { score:2, Num:1 }, { score:1, Num:2 }, { score:0, Num:0 } ]

    with the brackets included

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you control the input, use arrays instead of objects.
    Arrays will return strings without any work.

    Code:
    function sortScores(myscores){
    	return myscores.sort(function(a, b){
    		var n= a[0]-b[0];
    		if(n=== 0){
    			return a[1]-b[1];
    		}
    		return n;
    	});
    }
    var inp= [[2, 1], [1, 2], [0, 0]];
    sortScores(inp).join('\n')

    /* returned value: (String)
    0,0
    1,2
    2,1
    */

    /*
    If are working with objects, you need to make your own strings,
    */
    Code:
    function sortScoresString(myscores){
    	var temp, str= '[';
    	myscores= myscores.sort(function(a, b){
    		var n= a.score-b.score;
    		if(n=== 0){
    			return a.num-b.num;
    		}
    		return n;
    	});
    	for(var i= 0, L= myscores.length; i<L; i++){
    		temp= myscores[i];
    		str+= '{';
    		for(var p in temp){
    			if(temp.hasOwnProperty(p)){
    				str+= p+':'+temp[p]+',';
    			}
    		}
    		str= str.slice(0, -1)+'},'
    	}
    	return str.slice(0, -1)+']';
    }
    var inp= [{score: 2, num: 1},{score: 1, num: 2},{score: 0, num: 0}];
    sortScoresString(inp);

    /* returned value: (String)
    [{score:0,num:0},{score:1,num:2},{score:2,num:1}]
    */

  6. #6
    SitePoint Member stix_fs's Avatar
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    If you control the input, use arrays instead of objects.
    Arrays will return strings without any work.

    Code:
    function sortScores(myscores){
    	return myscores.sort(function(a, b){
    		var n= a[0]-b[0];
    		if(n=== 0){
    			return a[1]-b[1];
    		}
    		return n;
    	});
    }
    var inp= [[2, 1], [1, 2], [0, 0]];
    sortScores(inp).join('\n')

    /* returned value: (String)
    0,0
    1,2
    2,1
    */

    /*
    If are working with objects, you need to make your own strings,
    */
    Code:
    function sortScoresString(myscores){
    	var temp, str= '[';
    	myscores= myscores.sort(function(a, b){
    		var n= a.score-b.score;
    		if(n=== 0){
    			return a.num-b.num;
    		}
    		return n;
    	});
    	for(var i= 0, L= myscores.length; i<L; i++){
    		temp= myscores[i];
    		str+= '{';
    		for(var p in temp){
    			if(temp.hasOwnProperty(p)){
    				str+= p+':'+temp[p]+',';
    			}
    		}
    		str= str.slice(0, -1)+'},'
    	}
    	return str.slice(0, -1)+']';
    }
    var inp= [{score: 2, num: 1},{score: 1, num: 2},{score: 0, num: 0}];
    sortScoresString(inp);

    /* returned value: (String)
    [{score:0,num:0},{score:1,num:2},{score:2,num:1}]
    */
    Thanks so much, but if I wanted to sort the scores from ascending to descending order, how would I do that?

  7. #7
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Well, in the original code:

    Code:
    myscores= myscores.sort(function(a, b){
    This is using the sort() function (which does dictionary sort) with another function to sort numbers.

    Code:
    		var n= a[0]-b[0];          //for sorting the "scores"
    		if(n=== 0){                //if scores are equal
    			return a[1]-b[1]; //then sort by the "nums"
    		}
    		return n;
    Here the order is set by basically still doing var something = a minus b.

    I would assume you could just do b minus a instead for a reverse number sort.

    Javascript also has a reverse() function, which does the same as sort() for dictionary sorts, but backwards. But since this is a number sort, I figure just the b minus a thing would do the trick.

  8. #8
    SitePoint Member stix_fs's Avatar
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What other in built functions can i use instead of "hasOwnProperty"

  9. #9
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    I have to correct myself above:
    you'd keep a-b to reverse the sort, but change the order of the args in the function:
    sort(b,a)

    propertyIsEnumerable can tell you what's an actual property of your object, rather than something inherited from some ancestor object.


Tags for this Thread

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
  •