sortWith似乎没有按预期工作

时间:2012-09-28 18:48:53

标签: scala sorting calendar

事件类定义

  class event extends MongoRecord[event] with MongoId[event]{
      def meta = event
      object title extends StringField(this, 70)
      object description extends StringField(this, 200)
      object date_time extends DateTimeField(this)
      object email extends StringField(this, 70)
      object placeName extends StringField(this, 70)
      object placeLat extends DoubleField(this) 
      object placeLon extends DoubleField(this) 
      object placeCity extends StringField(this, 100)


      }
object event extends event with MongoMetaRecord[event] 

从文本存储日期:

val event_temp = event.createRecord
val cal = Calendar.getInstance()
val dashDate = new SimpleDateFormat("yyyy/MM/dd HH:mm")
cal.setTime(dashDate.parse(String))
event_temp.date_time(cal)
event_temp.save

获取事件和排序

val events= event.findAll
events.sortWith(_.date_time.is before _.date_time.is)

事件是List [event],而date_time.is则返回java.util.Calendar。 它返回未排序的List [event]。

1 个答案:

答案 0 :(得分:1)

我认为Scala 2.9.1没有问题:

scala> def d(y:Int) = { val c = Calendar.getInstance; c.setTime(new Date(y,0,0)); c }
warning: there were 1 deprecation warnings; re-run with -deprecation for details
d: (y: Int)java.util.Calendar

scala> val cs = d(100) :: d(1) :: d(1000) :: d(150) :: Nil 
// List[java.util.Calendar] .. removed long and useless

scala> val cs2 = cs sortWith { _ before _ } 
// List[java.util.Calendar] .. removed long and useless

scala> cs map { _.getTimeInMillis }
res21: List[Long] = List(946605600000, -2177528012000, 29347927200000, 2524528800000)

scala> cs2 map { _.getTimeInMillis }
res22: List[Long] = List(-2177528012000, 946605600000, 2524528800000, 29347927200000)