1

在 AS3 中,如何按字母顺序对该列表进行排序?我不是编程专家,我很难弄清楚这一点。

var searchObject:Object = new Object();
var mapXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("interactive-map.xml"));
xmlLoader.addEventListener(Event.COMPLETE, parseXML);

function parseXML(event:Event):void {
    mapXML = new XML(event.target.data);
    for each (var locationElement:XML in mapXML.maplocation) {
        searchObject.locationname = locationElement.attribute("locationname");
        searchObject.category = locationElement.attribute("category");
        trace("Location Name: " + searchObject.locationname);
    }
}
4

3 回答 3

1

首先,您需要将元素推送到数组/向量。然后您可以使用 sortOn 或 sort 方法对数组进行排序。检查 sortOn/sort 方法参考以查找排序选项。

var searchObject:Object = new Object();
var searchObjectArray:Array = new Array
var mapXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("interactive-map.xml"));
xmlLoader.addEventListener(Event.COMPLETE, parseXML);

function parseXML(event:Event):void {
    mapXML = new XML(event.target.data);
    for each (var locationElement:XML in mapXML.maplocation) {
        searchObject = new Object();
        searchObject.locationname = locationElement.attribute("locationname");
        searchObject.category = locationElement.attribute("category");
        searchObjectArray.push(searchObject);//pushing elements to array
    }
    searchObjectArray.sortOn("locationname")//sorting array
    for each ( var searchObjectElement:Object in searchObjectArray)
    {
        trace("Location name: " + searchObjectElement.locationname);//printing sorted array
    }
}
于 2012-05-20T12:56:45.217 回答
0

尝试这样的事情:

var nodes:Array = [];

for each (var locationElement:XML in mapXML.maplocation) {
   nodes[nodes.length] = locationElement;
}

nodes.sortOn(locationname);

我不确定这是否适用于 XML 数组元素。如果没有,请尝试将 sort() 与 compareFunction 一起使用

于 2012-05-20T03:15:51.907 回答