将数字5与字符串“5”分开的集合

时间:2010-04-08 19:39:47

标签: flex actionscript-3 flex3 advanceddatagrid

BackGround:我有一个高级数据网格。此ADG的数据提供程序是ArrayCollection。此AC的ID字段上有一个分组集合。

此AC中几个项目的示例AC var名称为“arcTemplates”:

(mx.collections::ArrayCollection)#0
  filterFunction = (null)
  length = 69
  list = (mx.collections::ArrayList)#1
    length = 69
    source = (Array)#2
      [0] (Object)#3
        abbreviation = "sore-throat"
        insertDate = "11/16/2009"
        name = "sore throat"
        templateID = 234
        templateType = "New Problem"
        templateTypeID = 1
     [32] (Object)#35
        abbreviation = 123
        insertDate = "03/08/2010"
        name = 123
        templateID = 297
        templateType = "New Problem"
        templateTypeID = 1
     [55] (Object)#58
        abbreviation = 1234
        insertDate = "11/16/2009"
        name = 1234
        templateID = 227
        templateType = "Exam"
        templateTypeID = 5
     [56] (Object)#59
        abbreviation = "breast only"
        insertDate = "03/15/2005"
        name = "breast exam"
        templateID = 195
        templateType = "Exam"
        templateTypeID = 5

导致分组的Flex代码示例:

<mx:AdvancedDataGrid displayItemsExpanded="true" id="gridTemplates">
  <mx:dataProvider>
    <mx:GroupingCollection id="gc" source="{arcTemplates}">
      <mx:Grouping >
        <mx:GroupingField name="templateTypeID" compareFunction="gcSort">

GC排序功能:

public function gcSort(a:Object, b:Object):int{
    return ObjectUtil.stringCompare(String(a.templateTypeID + a.name).toLowerCase(), 
                                    String(b.templateTypeID + b.name).toLowerCase());
}

问题:在我的AC示例中,有一些项目,项目0,32和56正确排序并分组到他们的templateTypeID,但项目55做了一些奇怪的事情。它似乎对数字5而不是字符串“5”进行排序/分组。变得陌生。如果我将name属性更改为包含文本(如此1234x),则将其正确排序/分组到字符串“5”

问题:这里发生了什么以及如何解决?

1 个答案:

答案 0 :(得分:2)

如果我信任您的跟踪,您会看到name=1234没有引用,因此被视为Number

当您使用gcSort String(a.templateTypeID + a.name)时,实际上这次添加两个数字(5+1234)并将它们转换回String =&gt; "1239"

您可以做的是首先将您的名字转换为字符串,然后进行连接:

(a.templateTypeID + a.name.toString()).toLowerCase()
相关问题