Sort multidimensional arrays in AS3 (numerically)


/ Published in: ActionScript 3
Save to your folder(s)

I have fixed and improved some code I found on the Internet


Copy this code and paste it in your HTML
  1. public function orderNumerically(a, b):Number{
  2. // Change a[0] & b[0] by a[i] & b[i]
  3. // (where i is the array index you want to use to sort the multidimensional array)
  4. var num1 = Number(a[0]);
  5. var num2 = Number(b[0]);
  6. if(num1<num2){
  7. return -1;
  8. }else if(num1>num2) {
  9. return 1;
  10. }else{
  11. return 0;
  12. }
  13. }
  14. the_array.sort(orderNumerically);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.