actionscript - sorting [with an external javascript]


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

an external javascript sort function, which you can import into flash to help sort arrays of objects. include the script thus:

#include "objSort.js"


Copy this code and paste it in your HTML
  1. /*
  2. objSort v 1.1
  3. copyright 2006 Thomas Frank
  4.  
  5. This program is free software under the terms of the
  6. GNU General Public License version 2 as published by the Free
  7. Software Foundation. It is distributed without any warranty.
  8. */
  9.  
  10. tfObjSort={
  11. init:function(){
  12. Array.prototype.objSort=function(){
  13. tfObjSort.setThings(this);
  14. var a=arguments;
  15. var x=tfObjSort;
  16. x.a=[];x.d=[];
  17. for(var i=0;i<a.length;i++){
  18. if(typeof a[i]=="string"){x.a.push(a[i]);x.d.push(1)};
  19. if(a[i]===-1){x.d[x.d.length-1]=-1}
  20. }
  21. return this.sort(tfObjSort.sorter);
  22. };
  23. Array.prototype.strSort=function(){
  24. tfObjSort.setThings(this);
  25. return this.sort(tfObjSort.charSorter)
  26. }
  27. },
  28. sorter:function(x,y){
  29. var a=tfObjSort.a
  30. var d=tfObjSort.d
  31. var r=0
  32. for(var i=0;i<a.length;i++){
  33. if(typeof x+typeof y!="objectobject"){return typeof x=="object"?-1:1};
  34. var m=x[a[i]]; var n=y[a[i]];
  35. var t=typeof m+typeof n;
  36. if(t=="booleanboolean"){m*=-1;n*=-1}
  37. else if(t.split("string").join("").split("number").join("")!=""){continue};
  38. r=m-n;
  39. if(isNaN(r)){r=tfObjSort.charSorter(m,n)};
  40. if(r!=0){return r*d[i]}
  41. }
  42. return r
  43. },
  44. charSorter:function(x,y){
  45. if(tfObjSort.ignoreCase){x=x.toLowerCase();y=y.toLowerCase()};
  46. var s=tfObjSort.chars;
  47. if(!s){return x>y?1:x<y?-1:0};
  48. x=x.split("");y=y.split("");l=x.length>y.length?y.length:x.length;
  49. var p=0;
  50. for(var i=0;i<l;i++){
  51. p=s.indexOf(x[i])-s.indexOf(y[i]);
  52. if(p!=0){break};
  53. };
  54. if(p==0){p=x.length-y.length};
  55. return p
  56. },
  57. setThings:function(x){
  58. this.ignoreCase=x.sortIgnoreCase;
  59. var s=x.sortCharOrder;
  60. if(!s){this.chars=false;return true};
  61. if(!s.sort){s=s.split(",")};
  62. var a="";
  63. for(var i=1;i<1024;i++){a+=String.fromCharCode(i)};
  64. for(var i=0;i<s.length;i++){
  65. z=s[i].split("");
  66. var m=z[0]; var n=z[1]; var o="";
  67. if(z[2]=="_"){o=n+m} else {o=m+n};
  68. a=a.split(m).join("").split(n).join(o);
  69. };
  70. this.chars=a
  71. }
  72. };
  73. tfObjSort.init();

URL: http://www.thomasfrank.se/sorting_things.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.