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

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

问题描述

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



所以我正在编写自己的排序函数。有没有办法,我可以检查是否所有的价值观之一,如果不告诉它做的定期比较价值?

这是我有:

 函数myCompare(a:Object,b:Object,fields:Array = null):int 
{
if(String(a).toLowerCase()=='all')
{
return -1;

else
if(String(b).toLowerCase()=='all')
{
return 1;
}
//需要在这里返回默认比较结果吗?
}

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

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

有没有解决方案,我正在尝试做什么? <解决方案

解决方案

解决方案



我试过一些东西,我真的很惊讶它实际上工作,但这是我做的。 Sort类有一个名为internalCompare的私有函数。由于它是私人的,你不能称之为。但有一个名为compareFunction的getter函数,如果没有定义比较函数,则返回一个对internalCompare函数的引用。所以我做的是得到这个引用,然后调用它。

  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;
}
//需要在这里返回默认比较结果吗?
var s:Sort = new Sort();
var f:Function = s.compareFunction;
返回f.call(null,a,b,fields);
}


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?

Here is what I have:

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.

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天全站免登陆