Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-12-2004, 02:41 PM   #1
roele
Scripting is like drugs..
 
Join Date: Nov 2001
Location: Switzerland
Posts: 155
Default Special sort on an array of objects

Hi,

i'm working on a game where i need a special sort algorithm which i can't figure out.... I save the players into an array in form of an object. This containts the player data and his entered values. The player should guess how many medals a country will get a the olympic games... Then the object contains a field with the difference to the final value too.

Code:
player = [
{Name:"", Prename:"", Gold:9, DifGold:1, Silver:19, DifSilver:0, Bronze:15,
DifBronze:-3}
];
The problem now is to sort this array on the three fields DifGold, DifSilver and
DivBronze. I used an Array protoype from Keneth Bunch for sorting on 2 fields.
Works all well till now...(ok just with 2 fields but who cares). BUT if a player guessed less Gold than they really got it is ranked better than one who has guessed the right number! Of course -1 < 0 is mathematically correct but i need to rank all number less than 0 after all other positive values.

Any ideas how to solve this ... ?

Kind regards,
Roland Schaer
roele is offline   Reply With Quote
Old 07-13-2004, 06:40 AM   #2
roele
Scripting is like drugs..
 
Join Date: Nov 2001
Location: Switzerland
Posts: 155
Default

The script i use so far looks like:
Code:
Array.prototype.sortOn = function(p) {
    Array.$sortprop = p;
    var sortfunc = function (a,b) {
      var p = Array.$sortprop;
	  return b[p] < a[p]; 
    }
                             
    this.sort(sortfunc);
    delete Array.$sortprop;
}
Array.prototype.subSort = function(sMainKey, sSubKey){
	
	// declare data holders
	var copy = this.slice(0);
	var holder = new Object();
	var returned = new Array();

	
	// sort array on sMainKey
	copy.sortOn(sMainKey);
	
	// create seperate arrays to hold values sorted on sMainKey
	var i = this.length;
	while(i--){
		// field of obj doesnt exist add it
		if(holder[copy[i][sMainKey]] == undefined){ 
			holder[copy[i][sMainKey]] = new Array();
			holder[copy[i][sMainKey]].push(copy[i]);
		}
		// else add fields into it
		else{
			holder[copy[i][sMainKey]].push(copy[i]);
		}
	}

	// subsort the subarrays on sSubKey
	for (prop in holder){
		holder[prop].sortOn([sSubKey]);
	}
	
	// add all values into one array 
	for (prop in holder){
		returned = returned.concat(holder[prop]);
	}
	
	// return subsorted array
	return returned;
};
roele is offline   Reply With Quote
Old 07-13-2004, 04:22 PM   #3
littleRichard
Registered User
 
littleRichard's Avatar
 
Join Date: Feb 2003
Location: Florida
Posts: 289
Default

There are some problems with your code. you don't need to do anything complicated like overwrite sortOn.

What you need to do is use sort with a sortFuntion. I can't write specific code for you at the moment but here is an example of some code I wrote last week sorting on multiple fields.

It will group together objects with the same y value and then whithin those groups it will sort on x1.

ActionScript Code:
someData = [     {x1:43,  y:205},     {x1:-9,  y:225},     {x1:225, y:225},     {x1:36,  y:245},     {x1:-9,  y:265},     {x1:44,  y:285},     {x1:165, y:285},     {x1:118, y:305},     {x1:-9,  y:285} ]; someData.sort(this.mySort); function mySort(a, b) {     ay = a.y;     ax = a.x1;     by = b.y;     bx = b.x1;         if(ay < by)     {         return -1;     }     else if(ay > by)     {         return 1;     }     else     {         if(ax < bx)         {             return -1;         }         else if(ax > bx)         {             return 1;         }         else         {             return 0;         }     } }

The code that matters is what's inside the mySort function.

1) if you want 'a' to show up before 'b' then return -1
2) if you want 'a' to show up after 'b' then return 1
3) and if 'a' and 'b' are the same return 0

I'd recomend reading up on Array.sort in the as dictionary. It gives a pretty good explanation.

Cheers,
Rich
littleRichard is offline   Reply With Quote
Reply


Thread Tools
Display Modes 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 Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:03 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.