Sort a object array


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

Sorts an array of objects using the native sortOn method


Copy this code and paste it in your HTML
  1. var records:Array = new Array();
  2. records.push({name:"john", city:"omaha", zip:68144});
  3. records.push({name:"john", city:"kansas city", zip:72345});
  4. records.push({name:"bob", city:"omaha", zip:94010});
  5.  
  6. for(var i:uint = 0; i < records.length; i++) {
  7. trace(records[i].name + ", " + records[i].city);
  8. }
  9. // Results:
  10. // john, omaha
  11. // john, kansas city
  12. // bob, omaha
  13.  
  14. trace("records.sortOn('name', 'city');");
  15. records.sortOn(["name", "city"]);
  16. for(var i:uint = 0; i < records.length; i++) {
  17. trace(records[i].name + ", " + records[i].city);
  18. }
  19. // Results:
  20. // bob, omaha
  21. // john, kansas city
  22. // john, omaha
  23.  
  24. trace("records.sortOn('city', 'name');");
  25. records.sortOn(["city", "name"]);
  26. for(var i:uint = 0; i < records.length; i++) {
  27. trace(records[i].name + ", " + records[i].city);
  28. }
  29. // Results:
  30. // john, kansas city
  31. // bob, omaha
  32. // john, omaha

URL: http://board.flashkit.com/board/showthread.php?t=777257

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.