I have Following Object structure in scala (case classes) :
{
"accounts": [{
"accountManagement": {
"accountStatus": "submitted",
"accountManagementId": "1513684862218",
"submittedDate": "19/12/2017"
}
}]
}
Look at List "accounts". I want to sort this list on the basis of field "submittedDate" from "accountManagement". Note that submitted date is in string format.
I tried this but not working.
for (accountManagement: AccountManagement <- accountManagementList) {
try {
if(accountManagement.submittedDate != null && accountManagement.submittedDate.nonEmpty){
accountManagement.submittedDate = dateFormatter.parse(accountManagement.submittedDate)
}
}catch {
case e:Exception =>
}
accountManagementsNew = accountManagementsNew ::: List(accountManagement)
}
accountManagementsNew.sortBy(_.updatedDate.getTime)
Lets say your case class looked like this.
// Note that in scala its preferred to use Option to indicate nullable fields
case class AccountManagement(accountStatus: String,
accountManagementId: Long,
submittedDate: Option[String])
val accounts = List(
AccountManagement("submitted", 1L, Some("21/12/2017")),
AccountManagement("submitted", 2L, Some("19/12/2017")),
AccountManagement("submitted", 3L, None),
AccountManagement("submitted", 4L, Some("20/12/2017"))
)
val dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy")
You can either define an implicit ordering that you want to use in this context
implicit val localDateOrdering: Ordering[LocalDate] = Ordering.by(_.toEpochDay)
accounts.filterNot(_.submittedDate.isEmpty) sortBy {
case AccountManagement(_, _, Some(submittedDateString)) => LocalDate.parse(submittedDateString, dtf)
}
or you can directly specify that you would like to use the millisecond representation of said date to sort your data set
accounts.filterNot(_.submittedDate.isEmpty) sortBy {
case AccountManagement(_, _, Some(submittedDateString)) => LocalDate.parse(submittedDateString, dtf).toEpochDay
}
If I understand your requirement correctly, you can first assemble a list of AccountManagementNew
from AccountManagement
by converting the String-type date to LocalDate
, then perform the sorting by date as shown below. Note that Try
is used to handle Success/Failure
cases.
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import scala.util.{Try, Success, Failure}
case class AccountManagement(
accountStatus: String, accountManagementId: String, submittedDate: String
)
case class AccountManagementNew(
accountStatus: String, accountManagementId: String, updatedDate: LocalDate
)
val accountManagementList = List[AccountManagement](
AccountManagement("submitted", "1513684862218", "19/12/2017"),
AccountManagement("submitted", "1513684862219", "09/01/2018"),
AccountManagement("submitted", "1513684862220", "29/11/2017")
)
val datePattern = DateTimeFormatter.ofPattern("dd/MM/yyyy")
// Assemble a list of the AccountManagementNew case class
val amNewList =
for (am <- accountManagementList) yield {
Try( LocalDate.parse(am.submittedDate, datePattern) ) match {
case Success(d) =>
AccountManagementNew(am.accountStatus, am.accountManagementId, d)
case Failure(_) =>
AccountManagementNew(am.accountStatus, am.accountManagementId, LocalDate.MIN)
}
}
// Use `LocalDate.toEpochDay` for date ordering
implicit val dateOrdering = Ordering.by{d: LocalDate => d.toEpochDay}
amNewList.sortBy(_.updatedDate)
// res1: List[AccountManagementNew] = List(
// AccountManagementNew(submitted,1513684862220,2017-11-29),
// AccountManagementNew(submitted,1513684862218,2017-12-19),
// AccountManagementNew(submitted,1513684862219,2018-01-09)
// )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.