Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 57 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. AS3: Sort array descending while keeping strings in correct order
  2. var d:Array;
  3. var e:Array;
  4. d = "827-Harry".split("-");
  5. d.push("918-John".split("-"));
  6. trace(d)
  7.    
  8. var highscores:Array = new Array();
  9. highscores.push({score: 827, player: "John"});
  10. highscores.push({score: 918, player: "Harry"});
  11. highscores.sortOn("score", Array.DESCENDING | Array.NUMERIC);
  12.  
  13. for (var i:int = 0; i < highscores.length; i++)
  14. {
  15.     trace(highscores[i].score, highscores[i].player);
  16. }
  17.    
  18. public function sortArray(arrUnsorted:Array):Array
  19. {
  20.     var arrLocal:Array = new Array();
  21.     var arrSorted:Array = new Array();
  22.  
  23.     for (var i:int = 0; i < arrUnsorted.length; i += 2)
  24.     {
  25.         arrLocal.push( { score:int(arrUnsorted[i]), name:arrUnsorted[i + 1] } );
  26.     }
  27.  
  28.     arrLocal.sortOn("score", Array.DESCENDING | Array.NUMERIC)
  29.  
  30.     for each(var obj:Object in arrLocal)
  31.     {
  32.         arrSorted.push(String(obj.score), obj.name);
  33.     }
  34.  
  35.     return arrSorted;
  36. }
  37.    
  38. var arrUnsorted:Array = ["827", "Harry", "918", "John"];
  39. var arrSorted:Array = sortArray(arrUnsorted);
  40.  
  41. trace(arrSorted); // 918,John,827,Harry
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top