javascript JQUERY 对对象数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7104024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:59:00  来源:igfitidea点击:

JQUERY Sorting an Array of Objects

javascriptjquery

提问by Adam

I have an array that is holding a number of objects.

我有一个包含多个对象的数组。

Array:

大批:

 var activeMembers=[];

The DIV objects in the above array look like as follows - each was added one at a time:

上面数组中的 DIV 对象如下所示——每次添加一个:

 <div id="mary" class="chatmember 1011"></div>
 <div id="steven" class="chatmember 1051"></div>
 <div id="adam" class="chatmember 1701"></div>
 <div id="bob" class="chatmember 1099"></div>
 <div id="peter" class="chatmember 1123"></div>

Is there a quick way to sort theses DIV objects in the array by the ID from A-Z?

有没有一种快速的方法可以通过 AZ 的 ID 对数组中的这些 DIV 对象进行排序?

thx

谢谢

回答by jfriend00

Since there are so many silly implementations being proposed that are using jQuery when it is only a waste of resources, I'll propose my own. This is just a straight javascript sort of an array by a property of the object in the array. To do that you just use the array sort method with a custom comparison function that does an alpha comparison of the id value. There is no reason or advantage to involve jQuery in this at all.

由于有很多愚蠢的实现被提议使用 jQuery,而这只是浪费资源,我将提出我自己的实现。这只是通过数组中对象的属性对数组进行的直接 javascript 排序。为此,您只需将数组排序方法与自定义比较函数一起使用,该函数对 id 值进行 alpha 比较。完全没有理由或优势让 jQuery 参与其中。

activeMembers.sort(function(a, b) {
   var aID = a.id;
   var bID = b.id;
   return (aID == bID) ? 0 : (aID > bID) ? 1 : -1;
});

Note, as requested in the question, this sorts a list of div references in the array. It does not sort the objects in the layout of the page. To do that, you'd have to sort the references list, then rearrange the divs in the page according to the new array order.

请注意,按照问题的要求,这会对数组中的 div 引用列表进行排序。它不会对页面布局中的对象进行排序。为此,您必须对引用列表进行排序,然后根据新的数组顺序重新排列页面中的 div。

If you can rely on the fact that no two IDs are ever the same in your own HTML (since you are never supposed to have two objects with the same ID), you can shorten and speed up the custom sort function to just be this:

如果您可以依赖这样一个事实,即您自己的 HTML 中没有两个 ID 是相同的(因为您永远不应该有两个具有相同 ID 的对象),您可以缩短和加速自定义排序功能,如下所示:

activeMembers.sort(function(a, b) {
   return (a.id > b.id) ? 1 : -1;
});

回答by jerjer

Try this:

试试这个:

activeMembers.sort(function(a,b){
   var aid = $(a).attr('id');
   var bid = $(b).attr('id');

   return (aid==bid) ? 0 : (aid > bid) ? 1 : -1;

});

回答by Stefan Mai

$('.chatmember').sort(function(a,b){ return a.id > b.id; })...

Returns (in Firebug):

返回(在 Firebug 中):

[div#adam.chatmember, div#bob.chatmember, div#mary.chatmember, div#peter.chatmember, div#steven.chatmember]

回答by user460667

You can use the tinysort plugin )(http://plugins.jquery.com/project/TinySort)

您可以使用 tinysort 插件 )(http://plugins.jquery.com/project/TinySort)

e.g.

例如

$("#parentName > div").tsort("",{attr:"id"});

回答by Sheepy

There is no quick way. You can use generic sorter that allow you to provide comparator, or you can write a custom sorter.

没有快速的方法。您可以使用允许您提供比较器的通用排序器,或者您可以编写自定义排序器。

See herefor an example.

有关示例,请参见此处