有人能让我了解sortedArrayUsingFunction 的工作原理吗?
我的数组具有以下内容:
{
"4eb57e72c7e24c014f000000" : {
"_id" : {
"$id" : "4eb57e72c7e24c014f000000"
},
"author" : "tim",
"comments" : [],
"created": {
"sec" : 1320517234,
"used" : 856000
},
"picture" : "http://someurl.com",
"text" : "this is a test",
"title" : "test",
"type" : ["test"]
}
我想按创建的(秒值)排序,
这就是我所拥有的......我只是不知道sortedArrayUsingFunction是如何工作的。我的意思是我在比较函数中比较什么?
jokesArray = [unSortedContentArray sortedArrayUsingFunction:Sort_Created_Comparer context:self];
NSInteger Sort_Created_Comparer(id num1, id num2, void *context)
{
int v1 = [num1 getSecFromJSONValue];
int v2 = [num2 getSecFromJSONValue];
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}
my array has the following:
{
"4eb57e72c7e24c014f000000" : {
"_id" : {
"$id" : "4eb57e72c7e24c014f000000"
},
"author" : "tim",
"comments" : [],
"created": {
"sec" : 1320517234,
"used" : 856000
},
"picture" : "http://someurl.com",
"text" : "this is a test",
"title" : "test",
"type" : ["test"]
}
I want to sort by created (sec value)
this is what I have....I just do not know how sortedArrayUsingFunction works. I mean what am I comparing in the compare function??
jokesArray = [unSortedContentArray sortedArrayUsingFunction:Sort_Created_Comparer context:self];
NSInteger Sort_Created_Comparer(id num1, id num2, void *context)
{
int v1 = [num1 getSecFromJSONValue];
int v2 = [num2 getSecFromJSONValue];
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
num1
和num2
是数组的 2 个元素,上下文是一个对象,您可以将其传递到函数中以帮助您进行排序。您的函数将在数组上多次调用,排序结果将在新数组中返回给您。
这就是你想知道的吗?
num1
andnum2
are 2 elements of your array and context is an object that you can pass in to your function to help you with the sort.Your function will be called many times on the array and the result of the sort will be returned to you in a new array.
Is that what you are wondering?
调用比较函数来一次比较 NSArray 中的两个值。这就是排序的完成方式。
The comparison function is called to compare two values at a time from your NSArray. This is how the sorting is done.
比较函数是告诉排序算法您希望如何对对象进行排序的方式。没有它,它就无法知道最终的顺序应该是什么。
在您的示例代码中,参数名称
num1
和num2
非常具有误导性。将它们更准确地命名为object1
和object2
。如果object1
应该出现在object2
之前,则返回NSOrderedAscending
。如果它应该在NSOrderedDescending
之后,否则返回NSOrderedSame
。为了说明这一点,下面是一个按年龄(从最低到最高)对假设的
Person
对象进行排序的示例。请注意,我什至没有使用上下文参数,因为我的对象本身有足够的信息来确定顺序。
如果我希望它们按高度降序排序,那么我可以传递以下内容:
非常重要的一件事是,如果参数以其他顺序传递,您的函数应该返回一致的结果。例如,如果
tallest_first(adam, joe, NULL)
返回NSOrderedDescending
,则tallest_first(joe, adam, NULL)
应返回NSOrderedAscending< /代码>。如果不是,你的比较函数就会自相矛盾。
The comparison function is your way of telling the sort algorithm how you want your objects ordered. Without it, it has no way of knowing what the final order should be.
In your example code the argument names
num1
andnum2
are very misleading. They would be more accurately namedobject1
andobject2
. Ifobject1
should come beforeobject2
then returnNSOrderedAscending
. If it should come after thenNSOrderedDescending
otherwise returnNSOrderedSame
.To illustrate, here is an example that sorts hypothetical
Person
objects by age (lowest to highest).Notice that I didn't even use the context parameter as my objects themselves had sufficient information to determine the order.
If I instead want them to be sorted by descending height then I could pass the following:
One thing that is very important is that your function should return a consistent result if the arguments are passed in the other order. For example if
tallest_first(adam, joe, NULL)
returnsNSOrderedDescending
thentallest_first(joe, adam, NULL)
should returnNSOrderedAscending
. If not your comparison function contradicts itself.