Flex:排序——编写自定义的 compareFunction? [英] Flex: Sort -- Writing a custom compareFunction?

查看:18
本文介绍了Flex:排序——编写自定义的 compareFunction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在按字母顺序对 XMLListCollection 进行排序.我有一个问题.如果值为ALL",我希望它在列表中排在第一位.在大多数情况下,这已经发生了,但数字值在ALL"之前排序.我希望ALL"始终是我的 dataProvider 中的第一个选择,然后按字母顺序排列.

OK, I am sorting an XMLListCollection in alphabetical order. I have one issue though. If the value is "ALL" I want it to be first in the list. In most cases this happens already but values that are numbers are being sorted before "ALL". I want "ALL" to always be the first selection in my dataProvider and then the rest alphabetical.

所以我正在尝试编写自己的排序函数.有没有办法可以检查其中一个值是否为全部,如果不是,则告诉它对这些值进行常规比较?

So I am trying to write my own sort function. Is there a way I can check if one of the values is all, and if not tell it to do the regular compare on the values?

这是我所拥有的:

function myCompare(a:Object, b:Object, fields:Array = null):int
{
    if(String(a).toLowerCase() == 'all')
    {
        return -1;
    }
    else 
        if(String(b).toLowerCase() == 'all')
        {
            return 1;
        }
    // NEED to return default comparison results here?
}

//------------------------------

var sort:Sort = new Sort();
sort.compareFunction = myCompare;

对于我正在尝试做的事情有解决方案吗?

Is there a solution for what I am trying to do?

推荐答案

嗯,我尝试了一些东西,我真的很惊讶它真的有效,但我就是这样做的.

Well I tried something out, and I am really surprised it actually worked, but here is what I did.

Sort 类有一个私有函数,称为 internalCompare.由于它是私人的,因此您无法调用它.但是有一个叫做 compareFunction 的 getter 函数,如果没有定义比较函数,它返回一个对 internalCompare 函数的引用.所以我所做的就是获取这个引用然后调用它.

The Sort class has a private function called internalCompare. Since it is private you cannot call it. BUT there is a getter function called compareFunction, and if no compare function is defined it returns a reference to the internalCompare function. So what I did was get this reference and then call it.

private function myCompare(a:Object, b:Object, fields:Array = null):int
{
    if(String(a).toLowerCase() == 'all')
    {
        return -1;
    }
    else if(String(b).toLowerCase() == 'all')
    {
        return 1;
    }
    // NEED to return default comparison results here?
    var s:Sort = new Sort();
    var f:Function = s.compareFunction;
    return f.call(null,a,b,fields);
}

这篇关于Flex:排序——编写自定义的 compareFunction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆