Flex 新手 XMLList 问题 - 对 XML 和 XMLList 进行排序

发布于 2024-08-14 04:46:11 字数 271 浏览 2 评论 0原文

是否可以对 XMLList 进行排序?我可以找到的所有示例都会创建一个新的 XMLListCollection,如下所示:

MyXMLListCol = new XMLListCollection(MyXMLList);

我认为本例中的 XMLListCollection 没有对 XMLList 的任何引用,因此对其进行排序会使我的 XMLList 未排序,这是正确的吗?

如何直接对 XMLList 进行排序?

谢谢 ~迈克

Is it possible to sort an XMLList? All the examples I can find on it create a new XMLListCollection like this:

MyXMLListCol = new XMLListCollection(MyXMLList);

I don't think the XMLListCollection in this case has any reference to the XMLList so sorting it would leave my XMLList unsorted, is this correct?

How can I sort the XMLList directly?

Thanks
~Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如日中天 2024-08-21 04:46:11

所以我终于对我的搜索词进行了足够的修改,我实际上找到了一个答案。
使用我从这里获得的技术:
http://freerpad.blogspot.com/ 2007/07/more-hierarchical-sorting-e4x-xml-for.html

我能够想出这个:

public function sortXMLListByAttribute(parentNode:XML,xList:XMLList,attr:String):void{
//attr values must be ints
var xListItems:int = xList.length();
if(xListItems !=0){
    var sortingArray:Array = new Array();
    var sortAttr:Number = new Number();
    for each (var item:XML in xList){
        sortAttr = Number(item.attribute(attr));
        if(sortingArray.indexOf(sortAttr)==-1){
            sortingArray.push(sortAttr);
        }
        //piggy back the removal, just have to remove all of one localName without touching items of other localNames
        delete parentNode.child(item.localName())[0];
    }
    if( sortingArray.length > 1 ) {
        sortingArray.sort(Array.NUMERIC);
    }

    var sortedList:XMLList = new XMLList();
    for each(var sortedAttr:Number in sortingArray){
        for each (var item2:XML in xList){
            var tempVar:Number = Number(item2.attribute(attr));
            if(tempVar == sortedAttr){
                sortedList += item2
            }
        }
    }
    for each(var item3:XML in sortedList){
        parentNode.appendChild(item3);
    }
}
}

工作得相当快,并保持我的原始 XML 变量更新。我知道我可能只是为了不使用 XMLListCollection 而重新发明轮子,但我认为对 XML 和 XMLList 进行排序的能力非常重要

So I finally got my search terms altered enough I actually churned up an answer to this.
Using the technique I got from here:
http://freerpad.blogspot.com/2007/07/more-hierarchical-sorting-e4x-xml-for.html

I was able to come up with this:

public function sortXMLListByAttribute(parentNode:XML,xList:XMLList,attr:String):void{
//attr values must be ints
var xListItems:int = xList.length();
if(xListItems !=0){
    var sortingArray:Array = new Array();
    var sortAttr:Number = new Number();
    for each (var item:XML in xList){
        sortAttr = Number(item.attribute(attr));
        if(sortingArray.indexOf(sortAttr)==-1){
            sortingArray.push(sortAttr);
        }
        //piggy back the removal, just have to remove all of one localName without touching items of other localNames
        delete parentNode.child(item.localName())[0];
    }
    if( sortingArray.length > 1 ) {
        sortingArray.sort(Array.NUMERIC);
    }

    var sortedList:XMLList = new XMLList();
    for each(var sortedAttr:Number in sortingArray){
        for each (var item2:XML in xList){
            var tempVar:Number = Number(item2.attribute(attr));
            if(tempVar == sortedAttr){
                sortedList += item2
            }
        }
    }
    for each(var item3:XML in sortedList){
        parentNode.appendChild(item3);
    }
}
}

Works pretty fast and keeps my original XML variable updated. I know I may be reinventing the wheel just to not use an XMLListCollection, but I think the ability to sort XML and XMLLists can be pretty important

只为一人 2024-08-21 04:46:11

虽然没有与 Array.sortOn 函数等效的本机函数,但实现您自己的排序算法就足够简单了:

// Bubble sort.

// always initialize variables -- it save memory.
var ordered:Boolean = false;
var l:int = xmlList.length();
var i:int = 0;
var curr:XML = null;
var plus:XML = null;
while( !ordered )
{
    // Assume that the order is correct
    ordered = true;
    for( i = 0; i < l; i++ )
    {
        curr = xmlList[ i ];
        plus = xmlList[ i + 1 ];    

        // If the order is incorrect, swap and set ordered to false.
        if( Number( curr.@order ) < Number( plus.@order ) )
        {
            xmlList[ i ]     = plus;
            xmlList[ i + 1 ] = curr;
            ordered = false;
        }
    }
}

但是,实际上,使用 XMLListCollection 更容易且错误更少。此外,如果其他人正在阅读您的代码,他们会发现它更容易理解。请帮自己一个忙,避免在这方面重新发明轮子。

While there is no native equivalent to the Array.sortOn function, it is trivial enough to implement your own sorting algorithm:

// Bubble sort.

// always initialize variables -- it save memory.
var ordered:Boolean = false;
var l:int = xmlList.length();
var i:int = 0;
var curr:XML = null;
var plus:XML = null;
while( !ordered )
{
    // Assume that the order is correct
    ordered = true;
    for( i = 0; i < l; i++ )
    {
        curr = xmlList[ i ];
        plus = xmlList[ i + 1 ];    

        // If the order is incorrect, swap and set ordered to false.
        if( Number( curr.@order ) < Number( plus.@order ) )
        {
            xmlList[ i ]     = plus;
            xmlList[ i + 1 ] = curr;
            ordered = false;
        }
    }
}

but, realistically, it is far easier and less buggy to use XMLListCollection. Further, if someone else is reading your code, they will find it easier to understand. Please do yourself a favor and avoid re-inventing the wheel on this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文