'DataGrid'에 해당되는 글 1건

  1. 2008/06/19 JS용 SortManager

JS용 SortManager

JAVASCRIPT 2008/06/19 16:32 |
Array인데 Object를 가진 Array의 경우 array.sort()의 결과를 얻기 뒤해서
소팅 걸때 쓸려고 만들어 봤다...

var SortManager = {
  type : "String",
  target : "",
  toLowerString : function(a,b){
   var x = a[SortManager.target] ? a[SortManager.target].toLowerCase() : "";
   var y = b[SortManager.target] ? b[SortManager.target].toLowerCase() : "";
   return [x,y];
  },
  ASC : function(a,b){
   var x = "" , y ="";
   try{
    switch(SortManager.type){
     case "String" :
      var res = SortManager.toLowerString(a,b);
      x = res[0];
      y = res[1];
     break;
     default :
      x = a[SortManager.target];
      y = b[SortManager.target];
     break;
    }
   }catch(e){}
   return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  },
  DESC : function(a,b){
   var x = "" , y ="";
   try{
    switch(SortManager.type){
     case "String" :
      var res = SortManager.toLowerString(a,b);
      x = res[0];
      y = res[1];
     break;
     default :
      x = a[SortManager.target];
      y = b[SortManager.target];
     break;
    }
   }catch(e){}
   return ((x > y) ? -1 : ((x < y) ? 1 : 0));
  },
  RAND : function(a,b){
   return (Math.random()*10)*(Math.random() - .5);
  }
 };

사용법
var a = [{n:"2004.12.18",b:"b"},{n:"2008.12.14",b:"a"},{n:"2008.120.14",b:"a"}];
SortManager.type = "String";
SortManager.target = "n";

alert(a.sort(SortManager.DESC));
만드는 김에 Random Sorting도 만들어 봤다..

alert(a.sort(SortManager.RAND));
식으로 사용하면 된다.

ActionScript도 이와 비슷하게 구현이 가능하다..
예전에 이에 대해 적어 놓은 글이 있다.
2007/07/09 - [ACTIONSCRIPT] - [3.0]Using Custom Function Array random sort


크리에이티브 커먼즈 라이선스
Creative Commons License

'JAVASCRIPT' 카테고리의 다른 글

에잇! 사라져 버려 IE 라디오버튼 생성 오류 by Javascript  (8) 2008/08/14
Checkbox  (0) 2008/07/15
[펌]AJAX로 SOAP 웹 서비스 사용하기  (0) 2008/07/04
JS용 SortManager  (0) 2008/06/19
자바스크립트 압축기  (2) 2008/05/21
XMLHttpRequest  (0) 2008/05/21
Open Editor  (3) 2008/05/09
상식  (2) 2008/03/28

Trackback Address :: http://lovedev.tistory.com/trackback/256

댓글을 달아 주세요