问题描述
这已经快把我逼疯了,我需要一个Flash DataGrid列(没有的Flex),按日期排序。我试图给日期列中包含一种功能:
This has been driving me crazy, I need to sort a Flash DataGrid column (not Flex) by date. I have tried giving the date column a sort function like below:
colTwo.sortCompareFunction = sortDate;
和是这种功能:
private function sortDate ($obj1,$obj2) : int {
trace("created date in sort "+$obj1["created"]);
var t1:Array = $obj1["created"].toString().split("-");
var t2:Array = $obj2["created"].toString().split("-");
var t1dt:Number=(new Date(Number(t1[0]),Number(t1[1]),Number(t1[2]))).getTime();
var t2dt:Number=(new Date(Number(t2[0]),Number(t2[1]),Number(t2[2]))).getTime();
trace(t1dt);
if(t1dt < t2dt) {
return -1;
} else if(t1dt == t2dt) {
return 0;
} else {
return 1;
}
}
但是,这似乎仍然试图将列按字母顺序排序。
But this still seems to attempt to sort the column alphabetically.
任何帮助将是AP preciated。
Any help would be appreciated.
推荐答案
由redHouse71答案是确定的,因为它会给你一个正确的结果,但作为一个code例如......好吧,下面是另一种变体,它本质上是做同样的事情,但就是少罗嗦了。
The answer by redHouse71 is OK, because it would give you a correct result, but as a code example... well, below is another variant, which does essentially the same thing, but is less wordy.
private function sortDate(a:Object, b:Object):int
{
var difference:Number =
this.truncateDate(a) - this.truncateDate(b);
return difference / Math.abs(difference);
}
// replace Object with the proper type
private function truncateDate(object:Object):uint
{
return (object.created as Date).time * 0.0001;
}
编辑:但是你为什么要截断日期秒?此外,它没有必要返回严格-1,0,1,基本上你可以破除只是 this.truncateDate(一) - this.truncateDate(B)
- 我添加的舍入,使之表现为原始应答
but why do you have to truncate the date to seconds? Also, it's not necessary to return strictly -1, 0, 1, basically you could do away with just this.truncateDate(a) - this.truncateDate(b)
- I added the "rounding" to make it behave as the original answer.
这篇关于在Flash AS3的DataGrid排序日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!