Actionscript:按日期排序ArrayCollection:YYYY-MM-DD

时间:2009-12-08 16:38:46

标签: flex actionscript-3 sorting date arraycollection

我有一个对象的ArrayCollection。每个对象都有以下键/值:

{date: 2009-12-01, visits=13555, bouceRate=45}
{date: 2009-12-05, visits=46955, bouceRate=45}
{date: 2009-12-06, visits=13685, bouceRate=45}
{date: 2009-12-02, visits=13685, bouceRate=45}
{date: 2009-12-04, visits=68755, bouceRate=45}
{date: 2009-12-03, visits=35875, bouceRate=45}

我需要按日期对此ArrayCollection进行排序,因此它将从过去到现在 - 就像这样:

{date: 2009-12-01, visits=13555, bouceRate=45}
{date: 2009-12-02, visits=13685, bouceRate=45}
{date: 2009-12-03, visits=35875, bouceRate=45}
{date: 2009-12-04, visits=68755, bouceRate=45}
{date: 2009-12-05, visits=46955, bouceRate=45}
{date: 2009-12-06, visits=13685, bouceRate=45}

我尝试过以下操作但没有优先权(不排序):

var dateSort:Sort = new Sort();
    dateSort.fields = [new SortField("date", false, false, true)];

newAreaChartData.sort = dateSort;
newAreaChartData.refresh();

// traceout
for (var i:int = 0; i <newAreaChartData.length; i++)
    trace ("Object #" + i + ": " + ObjectUtil.toString(newAreaChartData.getItemAt(i)));

3 个答案:

答案 0 :(得分:1)

这对我有用:

for(i = 0; i < newAreaChartData.length; ++i) {
    newAreaChartData[i].formattedDate = getActualDate(newAreaChartData[i].date);
    newAreaChartData[i].dateTime =    newAreaChartData[i].formattedDate.time;
    trace(newAreaChartData[i].dateTime);
}

    var dateSort:Sort = new Sort();
    dateSort.fields = [new SortField("dateTime", false, false, true)];
    newAreaChartData.sort = dateSort; 
    newAreaChartData.refresh();

for (var i:int = 0; i <newAreaChartData.length; i++)
    trace ("Object #"+ i + ": " + ObjectUtil.toString(newAreaChartData.getItemAt(i)));

答案 1 :(得分:1)

您创建SortField的方式:

new SortField("date", false, false, true)

API开始,最后一个参数应该意味着您希望以数字方式而不是字符串对值进行排序。

对象中“日期”字段的类型是什么?如果是字符串,那么您可能希望按字母顺序对事物进行排序

new SortField("date", false, false, null)

希望这有帮助

PH

答案 2 :(得分:0)

我不相信在你调用ArrayCollection.refresh()之前应用排序:

newAreaChartData.sort = dateSort;
newAreaChartData.refresh();