ArrayCollection排序

带排序的子元素结构体

public class ArrayCollectionSort

    {
        /**
         * @param sort1 主要排序字段
         * @param sort2 辅助排序字段
         */
        public function ArrayCollectionSort(sort1:int,sort2:Number)
        {
            _key_main=sort1;
            _key_other=sort2;
        }
        private var _key_main:int;        
        private var _key_other:Number;

        public function get key_other():Number
        {
            return _key_other;
        }


        public function get key_main():int
        {
            return _key_main;
        }        

        public function toString():String
        {
            // TODO Auto Generated method stub
            return "key_main:"+_key_main+",key_other"+_key_other;
        }

    }

列表管理类

public class ArrayCollectionSorts
    {
        public var list:ArrayCollection;
        public function ArrayCollectionSorts()
        {
            list=new ArrayCollection();            
            var sort:Sort = new Sort();
            sort.compareFunction = sortFunction;
            list.sort = sort;
            list.refresh();//调用ArrayCollection的refresh方法后的addItem才进行排序
        }
        
        
        /**
         * @return -1,如果在已排序序列中 Object a 应显示在 Object b 之前。
         * 0,如果 Object a 等于 Object b。
         * 1,如果在已排序序列中 Object a 应显示在 Object b 之后。

         */
        public function sortFunction(a:ArrayCollectionSort, b:ArrayCollectionSort, array:Array = null):int {
            if(a.key_main>b.key_main){
                return 1;
            }else{
                if(a.key_main<b.key_main){
                    return -1;
                }else{
                    if(a.key_other>b.key_other){
                        return 1;
                    }else{
                        if(a.key_other<b.key_other){
                            return -1;
                        }else{
                            return 0
                        }
                    }
                }
            }
        }
        
        public function addItem(param:ArrayCollectionSort):void
        {
            // TODO Auto Generated method stub
            list.addItem(param);
        }
        
        public function size():int
        {
            // TODO Auto Generated method stub
            return list.length;
        }
        
        public function getItemAt(i:int):ArrayCollectionSort
        {
            // TODO Auto Generated method stub
            return list.getItemAt(i) as ArrayCollectionSort;
        }
        
        public function refresh():void
        {
            // TODO Auto Generated method stub
            list.refresh();
        }
    }


注意:

  1. 调用ArrayCollection的refresh方法后的addItem才进行排序
  2. 排序方法中if else 返回-1,1,0情况必须写全

测试类中方法

        [Test]
        public function sort():void
        {
            var list:ArrayCollectionSorts=new ArrayCollectionSorts();
            list.addItem(new ArrayCollectionSort(1,1.0));
            list.addItem(new ArrayCollectionSort(1,1.1));
            list.addItem(new ArrayCollectionSort(2,1.0));
            list.addItem(new ArrayCollectionSort(1,0.9));
            list.addItem(new ArrayCollectionSort(2,0.9));
            list.addItem(new ArrayCollectionSort(1,1.0));
            list.addItem(new ArrayCollectionSort(0,1.0));            
            list.addItem(new ArrayCollectionSort(3,1.0));
            
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值