按数组内容排序
我有一个如下所示的数组:
markerArray[i] = [i, title, cat];
该数组位于添加动态内容的 for 循环内
,因此结果可能如下所示:
markerArray[0] = [0, A title, A];
markerArray[1] = [1, A title new, B];
markerArray[3] = [3, A title, E];
markerArray[4] = [4, A title, A];
所以我的问题是我如何或是否可以对数组进行排序,以便输出基于在 A、BC 等或标题或从数组内部获取的任何内容上,请注意最后一个(类别)现在按字母顺序排列:
markerArray[0] = [0, A title, A];
markerArray[1] = [4, A title, A];
markerArray[2] = [1, A title new, B];
markerArray[3] = [3, A title, E];
这可能吗?
I have an array looking like this:
markerArray[i] = [i, title, cat];
this array is within a for loop adding the content dynamic
so the result could be something like this:
markerArray[0] = [0, A title, A];
markerArray[1] = [1, A title new, B];
markerArray[3] = [3, A title, E];
markerArray[4] = [4, A title, A];
So my question is how can I or is it possible to sort the array so that the output would be based on A,B C etc or title or whatever taken from inside the array like this, note that the last (category) is in alphabetic order now:
markerArray[0] = [0, A title, A];
markerArray[1] = [4, A title, A];
markerArray[2] = [1, A title new, B];
markerArray[3] = [3, A title, E];
Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试 -
try -
您必须使用 Array 对象中提供的 sort 函数。
此函数/方法接受自定义用户函数来确定元素的排序方式。
有用信息: https://developer.mozilla.org/en/JavaScript/参考/Global_Objects/数组/排序
You have to use the sort function, available in the Array object.
This function/method accepts a custom user function to determine how the elements have to be sorted.
Useful info: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
使用自定义排序功能。阅读文档如何操作。
Use a custom sort function. Read a documentation how to do it.
您可以将比较函数作为参数传递给 Array.sort 方法,并将比较逻辑写入比较函数内。请参阅此处 https://developer.mozilla.org/en/JavaScript/Reference /Global_Objects/Array/sort
You can pass the compare function as a parameter to Array.sort method and write your comparison logic inside the compare function. See here https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
您可以尝试这种方法:
这将尝试对第一个元素进行排序,如果它们相同,它将尝试对第二个元素进行排序,依此类推对第三个元素进行排序。
[编辑]添加一些有趣的链接(也请参阅其他答案):
/
最简单的方法是什么?在 jQuery 中?
You could try this kind of stuff:
This will try to sort with the first element, if they are identical, it tries to sort with the second one, and so on with the third one.
[EDIT] Adding some interesting links (see other answers too):