Flex: ArrayCollection/ListCollectionView 排序

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

This entry was posted in ActionScript, Flex and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>