根据父对象的NSSet内的子元素对父对象进行排序 [英] Sort the Parent object based on a child element inside a NSSet of the parent object

查看:58
本文介绍了根据父对象的NSSet内的子元素对父对象进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有一对多的核心数据对象图关系,其中一个帐户可以具有许多日志(Account-Logs).我的目标是根据子对象的值(即日志中的日期)对我从提取请求中获得的帐户数组进行排序

 扩展帐户{@NSManaged公共变量account_id:字符串?@NSManaged公共变量日志:NSSet?}扩展SalesLogs {@NSManaged公用var created_date:字符串?@NSManaged公共变量is_modified:布尔} 

我的尝试像在吼叫.我想比较每个帐户的最后一个created_date,并返回一个已排序的Accounts数组

 让fetchData:[Accounts] =试试StorageManager.shared.fetchData(实体:"SalesLogs",模型:SalesLogs.self)让我们对数组排序:[Accounts] = fetchData.filter {($ 0.logs?.allObjects as![SalesLogs]).map {$ 0.created_date?">} .last == [true]} 

解决方案

首先,您需要确定要对哪些日志日期进行排序.如果是最新版本,则需要先找到它.

我建议您将 SalesLogs 更新为实际处理日期而不是字符串,因为您可以轻松比较日期并找到最大值(即最新日期).

 扩展SalesLogs {var createdDate:日期{让formatter = DateFormater()formatter.dateFormat = ...//为了简单起见,我避免使用可选的Date返回formatter.date(from:self.created_date?"))Date.distantPast}} 

为此,您需要在集合中查找最新日期,并使用该日期对数组进行排序:

  let sorted = fetchData.sorted {a,b in让logsA = a.logs为?设置< SalesLogs>让logsB = b.logs为?设置< SalesLogs>//找到最新的(aka max)createdDatelet dateA = logsA?.map {$ 0.createdDate} .max {$ 0<$ 1}let dateB = logsB?.map {$ 0.createdDate} .max {$ 0<$ 1}return(dateA ?? Date.distantPast)<(dateB ?? Date.distantPast)} 

I have a coredata object graph relationship of one to many where one Account could have many Logs (Account<->>Logs). My objective is to sort the array of Accounts that i get from the fetch request, based on the child object's value which is a date inside my Log

extension Account {
 @NSManaged public var account_id: String?
 @NSManaged public var logs: NSSet?
}

extension SalesLogs {
 @NSManaged public var created_date: String?
 @NSManaged public var is_modified: Bool

}

My attempt was as bellow. I wants to compare the last created_date of each Account, and returns a sorted Accounts array

 let fetchData : [Accounts]= try StorageManager.shared.fetchData(entity: "SalesLogs", model: SalesLogs.self)
 let sorted array:[Accounts] = fetchData.filter{($0.logs?.allObjects as! [SalesLogs]).map {$0.created_date ?? "" > }.last == [true]}

解决方案

First, you need to determine which of the logs dates you want to sort by. If it's the latest, then you'd need to find it first.

I suggest you update your SalesLogs to actually deal with dates instead of strings, because you could easily compare dates and find the maximum (i.e latest).

extension SalesLogs {

   var createdDate: Date {
      let formatter = DateFormater()
      formatter.dateFormat = ...

      // I'm avoiding an optional Date for simplicity
      return formatter.date(from: self.created_date ?? "") ?? Date.distantPast
   }
}

Armed with that, you'd need to find the latest date in a set, and use that to sort the array:

let sorted = fetchData.sorted { a, b in

   let logsA = a.logs as? Set<SalesLogs>
   let logsB = b.logs as? Set<SalesLogs>

   // find the latest (aka max) createdDate
   let dateA = logsA?.map{ $0.createdDate }.max{ $0 < $1 }
   let dateB = logsB?.map{ $0.createdDate }.max{ $0 < $1 }

   return (dateA ?? Date.distantPast) < ( dateB ?? Date.distantPast)
}

这篇关于根据父对象的NSSet内的子元素对父对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆