Flex: ArrayCollection/ListCollectionView 排序

Categories: Flex; Tagged with: ; @ October 16th, 2009 16:16

1. 初始化Sort

protected var sortCodes:Sort = new Sort();

2. 设定Sort的compareFunction;
sortCodes.compareFunction = compareFuncCodes

	/** 对代码进行排序*/
	protected function compareFuncCodes(firstCode:ICode, secondCode:ICode, fields:Array = null):int {
		var _entity:Entity = AppContext.getInstance().metaDomain.getEntityByName(_entitySystemName);
		var attrProgramCode:Attribute = _entity.getAttributeBySystemName("programCode");
		var attrSeqNo:Attribute = _entity.getAttributeBySystemName("seqNo");
		
		// 首先按照ProgramCode排序
		if(attrProgramCode != null) {
			if((firstCode as Object).programCode > (secondCode as Object).programCode) {
				return 1; //
			}else if((firstCode as Object).programCode < (secondCode as Object).programCode) {
				return -1;
			}
		}
		
		// 然后按照seqNo排序
		if(attrSeqNo != null) {
			if((firstCode as Object).seqNo > (secondCode as Object).seqNo) {
				return 1; //
			}else if((firstCode as Object).seqNo < (secondCode as Object).seqNo) {
				return -1;
			}
		}
		
		// 最后按照code排序
		if(firstCode.code > secondCode.code) {
			return 1;
		}else if(firstCode.code == secondCode.code) {
			return 0;
		}else {
			return -1;
		}
		
	}

3. 在ArrayCollection/ListCollectionView上使用刚才设定的排序:

codesAC.sort = sortCodes; // 设定排序
codesAC.refresh(); // 刷新ArrayCollection



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.