Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums Special sort on an array of objects
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 07-12-2004, 02:41 PM #1 roele Scripting is like drugs..   Join Date: Nov 2001 Location: Switzerland Posts: 155 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
 07-13-2004, 06:40 AM #2 roele Scripting is like drugs..   Join Date: Nov 2001 Location: Switzerland Posts: 155 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; };```
 07-13-2004, 04:22 PM #3 littleRichard Registered User     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. Cheers, Rich

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

All times are GMT. The time now is 02:46 PM.

///