array 에 해당하는 글1 개
2007/01/10   How to sort Objects(Class)

flash/Tip | 2007/01/10 00:01

sortOn (Array.sortOn method)

public sortOn(fieldName:Object, [options:Object]) : Array

Sorts the elements in an array according to one or more fields in the array. The array should have the following characteristics:

  • The array is an indexed array, not an associative array.
  • Each element of the array holds an object with one or more properties.
  • All of the objects have at least one property in common, the values of which can be used to sort the array. Such a property is called a field.

    If you pass multiple fieldName parameters, the first field represents the primary sort field, the second represents the next sort field, and so on. Flash sorts according to Unicode values. (ASCII is a subset of Unicode.) If either of the elements being compared does not contain the field that is specified in the fieldName parameter, the field is assumed to be undefined, and the elements are placed consecutively in the sorted array in no particular order.

    오브젝트를 일반적인 단일속성이 아닌 한개 이상의 여러 속성을 기준으로 정렬할시 사용되는 메서드이다. 오브젝트를 정렬할때 종종 사용하던 방법이였는데 이번 프로젝트를 진행하면서 클래스 자체를 정렬할때 사용해 보았는데 역시나 생각한 대로 작동하였다...^^

    클래스는 당연히 오브젝트의 일종이다. 따라서 클래스 안에 있는 멤버 변수들을 기준으로 정렬이 가능하다는 의미다. 자신이 클래스를 사용하여 이미지 갤러리같은 정렬의 기능이 있는 컨텐츠를 제작할시에 아주 유용한 기능일 것이다.


    // widget class : 정렬에 사용될 클래스

    class com.dstrict.UB.project.ces2007.widget.Widget {

      public var _id:String;
      public var _title:String;
      public var _date:Number;

      public function Widget(id:String,title:String,date:Number) {
      _id=id;
      _title=title;
      _date=date;
    }

    }


    // example code

    import com.dstrict.UB.project.ces2007.widget.Widget ;

    var widgetArr:Array=new Array();

    var widget1:Widget=new Widget("vkimone","widget1",20061213);
    var widget2:Widget=new Widget("kimkijeung","widget2",20061110);
    var widget3:Widget=new Widget("Tom","widget3",20061115);
    var widget4:Widget=new Widget("Jane","widget4",20061211);

    widgetArr.push(widget1);
    widgetArr.push(widget2);
    widgetArr.push(widget3);
    widgetArr.push(widget4);

    for(var i=0; i<widgetArr.length ; i++){
    trace(i+"  : "+widgetArr[i]._id+" : "+widgetArr[i]._title+"  ----------->"+widgetArr[i]._date);

    trace
    ("======================================");

    widgetArr.sortOn("_date",Array.NUMERIC| Array.DESCENDING);

    for(var i=0; i<widgetArr.length ; i++){
    trace(i+"  : "+widgetArr[i]._id+" : "+widgetArr[i]._title+"  ----------->"+widgetArr[i]._date);
    }


    //result
    0  : vkimone : widget1  ----------->20061213
    1  : kimkijeung : widget2  ----------->20061110
    2  : Tom : widget3  ----------->20061115
    3  : Jane : widget4  ----------->20061211
    ======================================
    0  : vkimone : widget1  ----------->20061213
    1  : Jane : widget4  ----------->20061211
    2  : Tom : widget3  ----------->20061115
    3  : kimkijeung : widget2  ----------->20061110



    각각의 아이디값과 제목 그리고 날짜 정보를 포함하는 widget 클래스에서 여러개의 객체를 생성한후
    배열에 넣은 다음 오브젝트 속성인 _date 를 기준으로 내림차순으로 숫자 정렬했을때의 결과값이다.
    각 클래스 속성을 기준으로 통채로 정렬이 된다.

    만역 클래스를 이용해 철저히 오브젝트들을 캡슐화(encapsulation)했다면 손쉽게 오브젝트 정렬을 사용할 수 있을 것이다.





  •  
     
    태그 : , ,
    이 글의 관련글(트랙백) 주소 :: http://kimkijeung.com/trackback/77

    Name 
    Password 
    Homepage 
      secret
    Comment 
      글쓰기


    [PREV] [1] [NEXT]

     
    전체 (105)
    flash (74)
    math&physics (4)
    programming (11)
    Flex2 (1)
    Mac (2)
    photo (0)
    project (6)
    주저리주저리 (3)
    유용한 자료들 (1)
    diary (0)
    Book (1)
    web (2)
    «   2009/01   »
            1 2 3
    4 5 6 7 8 9 10
    11 12 13 14 15 16 17
    18 19 20 21 22 23 24
    25 26 27 28 29 30 31