排序这与CoreData填充数组 [英] Sorting an array that's filled from CoreData
问题描述
我有哪些用户可以输入数值,并将它们存储到核心数据作为字符串文本字段。反过来,我检索这些值,并存储它们放入数组为整数。我想要做的排序是该阵列由用户,因为我试图绘制的值,看看随着时间的推移其走势存储值的顺序。眼下值从所有我可以告诉填充在一个完全随机的方式我的图表。
I have a text field in which users can input numerical values and store them to core data as strings. In turn I am retrieving those values and have stored them in an array as ints. What I want to do is sort that array by the order that the user stores the values because I am trying to graph the values to see their trend over time. Right now the values are populating my graph in a totally random manner from all I can tell.
var client: Client! = nil
var weights = []
func getWeights() {
var assessmentWeights: NSSet = client.assessment.valueForKey("weight") as NSSet
var weightSet: NSSet = assessmentWeights.valueForKey("integerValue") as NSSet
weights = weightSet.allObjects
println(weights)
}
任何帮助AP preciated! :)
Any help is appreciated! :)
推荐答案
如果您已经在各权重项,你可以(一)建立关联的时间戳的 NSFetchRequest
与排序描述符,也可以(b)以的NSSet
If you already have a timestamp associated with each weight entry you can either (a) create a NSFetchRequest
with sort descriptor or you can (b) sort the values you already have in NSSet
由于我没有看到太多code一起去,这里的选项的示例(二):
Since I do not see much code to go with, here's an example of option (b):
var weights = [Int]();
if let entries = client.assessment.allObjects as? [Assessment] {
let sorted = entries.sorted {
return $0.timestamp.compare($1.timestamp) == .OrderedAscending
}
for assessment in sorted {
if let intValue = assessment.weight.toInt() {
weights.append(intValue)
}
}
}
return weights
话虽如此,最好的选择,在我看来,是权重值存储为一个数值并使用 NSFetchRequest
(结果类型。 DictionaryResultType
)与时间戳排序描述得到你想要的值。
Having said this, the best option, in my opinion, is to store weight value as a numeric value and use NSFetchRequest
(result type .DictionaryResultType
) with sort descriptor on timestamp to get the values you want.
这篇关于排序这与CoreData填充数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!