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


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.

player = [
{Name:"", Prename:"", Gold:9, DifGold:1, Silver:19, DifSilver:0, Bronze:15,
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
Scripting is like drugs..
Join Date: Nov 2001
Location: Switzerland
Posts: 155

The script i use so far looks like:
Array.prototype.sortOn = function(p) {
    Array.$sortprop = p;
    var sortfunc = function (a,b) {
      var p = Array.$sortprop;
	  return b[p] < a[p]; 
    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
	// create seperate arrays to hold values sorted on sMainKey
	var i = this.length;
		// field of obj doesnt exist add it
		if(holder[copy[i][sMainKey]] == undefined){ 
			holder[copy[i][sMainKey]] = new Array();
		// else add fields into it

	// subsort the subarrays on sSubKey
	for (prop in holder){
	// 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
Registered User
littleRichard's Avatar
Join Date: Feb 2003
Location: Florida
Posts: 289

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.

littleRichard is offline   Reply With Quote

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 11:20 AM.

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.