了解阵列使用用户定义的函数排序或比较函数回调 [英] Understanding array sorting or comparison function callbacks using user-defined functions

查看:161
本文介绍了了解阵列使用用户定义的函数排序或比较函数回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ P $像我ptend不知道我在做什么,这我不知道。你能解释这需要回调的工作阵列如何排序和比较功能呢?

Pretend like I have no idea what I'm doing, which I don't. Can you explain how the array sorting and comparison functions which require callbacks work?

uasort例如()功能。我应该如何设计一个回调函数这个工作?什么是-1的意思,+ 1和0的时候我试图对数据进行排序?它会通过所有的数组元素扫描?或者是比这更有效?如果我想W来之前,B,而一,X之前来怎么办?你可以在这个看似简单的区域提供任何意见将是最AP preciated。

Take the uasort() function for example. How am I supposed to design a callback function to work with this? What is the meaning of "-1", "+1", and "0" when I'm trying to sort data? Will it scan through all array elements? Or is it more efficient than that? What if I want "w" to come before "b", while "a" comes before "x"? Any advice you can give in this seemingly simple area will be most appreciated.

推荐答案

我给它一个镜头!

有关的例子,让我们的名单。你我都知道如何按字母顺序排序的名单,并做了一百bazillion倍。同样适用于,比如说数字。计算机需要能够对数据进行排序,其中排序可能不那么直观。

For an example, let's take a list of names. You and I both know how to sort a list of names alphabetically, and done it a hundred bazillion times. Same thing applies to, say, numbers. Computers need to be able to sort data where the sorting may not be so intuitive.

有关数组排序回调函数涉及允许数组包含任意类型的数据,在回调说,数组中的任何两个项目是如何相互关联的供应规则的想法。以这种方式,一个排序算法可以采取数据的阵列,所说的回调功能重复,以确定哪些元素在其中顺序属于,并返回排序列表。这是一个回调的本质。

Callback functions for sorting arrays involve the idea of allowing the array to contain an arbitrary kind of data, with the callback supplying the "rules" for saying how any two items in that array relate to each other. In this way, a sorting algorithm can take that array of data, call the "callback" function repeatedly to determine which elements belong in which order, and return the sorted list. That's the very essence of a callback.

如果你想有一个字面的比喻,pretend你的手数据,以一个朋友的名单,他们只知道的方法的进行排序,但不知道如何在你的项目名单涉及。他们回拨给你,问这个问题:这两个因素是第一位的?而你,知道数据和规律,采取项目,将您的规则,然后告诉这位朋友的回答。最终,多次重复这一过程后,您的数据返回到您来分类的。

If you want a literal analogy, pretend you hand a list of data to a friend, and they only know a method for sorting, but don't know how the items in your list relate. They "call back" to you, asking the question "Which of these two elements comes first?" And you, knowing the data, and the rule, take the items, apply your rules, and then tell the friend the answer. Ultimately, after repeating this process multiple times, your data returns to you sorted.

在这样一个回调返回-1,0或1,返回说,当两个数据称为值-1表示件商品的precedes项目二,0说项目一等于项目二,和+1说:项目一个跟随项目两项。您只需提供该决定基于数据的规则,则返回该值比较。您可以定义任意数据集,并定义你希望你的问题中的任何precedence规则空间。顺便说一句,这就是面向对象编程的重要组成部分 - 我可以利用这个回调的想法实施的通用的复杂的排序算法的版本的从不知道或不关心有关的一种的数据进行排序 - 一切都因为用这个罐头排序机制,程序员会提供逻辑管道排序过程的需要。

In this way, a "callback" that returns -1, 0, or 1, returns a value that says, when called with two pieces of data, -1 says "item one precedes item two," 0 says "item one equals item two," and +1 says "item one follows item two." You simply supply the comparisons that determine which value is returned based on the rules for your data. You can define an arbitrary set of data and define whatever precedence rule you wish within your problem "space." As an aside, that's an important part of object-oriented programming - I can leverage this "callback" idea to implement a generic version of a complex sorting algorithm and never know or care about the kind of data being sorted - all because the programmer using this "canned" sorting mechanism will provide the "logical plumbing" the sort routine needs.

我认为这是一个在解释像样的射门,并且都在一个语言无关的方式呢! :)希望有所帮助。

I think that's a decent shot at an explanation, and all in a language-agnostic way, too! :) Hope that helps.

修改让我提供一个例子:

List:

Item #  Value
1        12
2        15
3         9
4        26
5         4

如果我开始在列表中找到最小的项目,我会用第1项开始,它比较对列表中的每个剩余项目。所以,第一遍,我比较项目1项2:

if I set out to find the smallest item in the list, I'd start with item 1, and compare it against each remaining item in the list. So, first pass, I'd compare Item 1 to Item 2:

compare(item(1),item(2))

这将返回-1,因为12的 precedes值的15.现在,我去的下次的列表中的项目:

which would return -1, because the value of 12 precedes 15. Now, I go to the next item in the list:

compare(item(1),item(3))

而这个时候返回1,因为12的如下的9,这意味着项目(3)现在最小的项目迄今发现。现在,我们比较正是如此:

which this time returns 1, because 12 follows 9, meaning item(3) is now the smallest item found so far. Now, we compare thusly:

compare(item(3),item(4))

在这种情况下,比较返回-1,因为9 precedes 26,导致我们最终的比较:

In this case, compare return -1, because 9 precedes 26, leading us to the final compare:

compare(item(3),item(5))

和此调用将返回+1,因为正如我们已经用尽列表中的所有项目9自带4.之后,我们知道(3)项是最小的项目。然后,我们会交换该项目为previously顶级项目,并重复开始项目的整个过程(2)。这是出了名的低效率的一个例子冒泡排序,但可以使用本说明的目的。这就是第一项和第二项引用从何而来。排序,不分语言,就像在计算机科学中的其他问题,可以归结为在小叮咬打破大问题,分拣归结为重复地从一个更大的列表比较两个项目。

And this call will return +1, because 9 comes after 4. As we've exhausted all items in the list, we know item(3) is the smallest item. We'd then swap that item for the "previously" top item, and repeat the entire process starting at item(2). This is an example of a notoriously inefficient "bubble sort," but works for the purposes of this illustration. That's where the "first item" and "second item" references come from. Sorting, regardless of language, and like any other problem in computer science, boils down to breaking down big problem in to small bites, and sorting boils down to repeatedly comparing two items from a larger list.

这篇关于了解阵列使用用户定义的函数排序或比较函数回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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