Jquery数据属性-排序&;删除重复项

Jquery数据属性-排序&;删除重复项,jquery,Jquery,我有许多聊天信息,其中包含一个PID,该PID位于数据库表中的第行,如下所示: 我需要做以下工作: $('div').sort(function (a, b) { return + a.getAttribute('data-pid') > + b.getAttribute('data-pid'); }).appendTo('#somewhere') .filter(function() { var id = this.getAttribute('data-pid')

我有许多聊天信息,其中包含一个PID,该PID位于数据库表中的第行,如下所示:

我需要做以下工作:

$('div').sort(function (a, b) {
   return + a.getAttribute('data-pid') > + b.getAttribute('data-pid');
}).appendTo('#somewhere')
  .filter(function() {
      var id = this.getAttribute('data-pid');
      // return the element if the previous sibling has the same PID
      return $(this).prev('[data-pid="'+id+'"]').length;
  }).remove();
  • 从最低到最高的顺序排列,底部保留所有无编号(空编号)
  • 删除任何重复项
我很不确定怎么做。我曾考虑:

sort(function(a, b) {

将它们放入数组,对数组进行排序,删除它们,然后追加排序后的数组

.each(function () { 
任何建议都很好


thx

JavaScript不应用于清除标记,最好的解决方案是防止生成重复元素,但是如果必须这样做,可以使用
排序
方法,如下所示:

$('div').sort(function (a, b) {
   return + a.getAttribute('data-pid') > + b.getAttribute('data-pid');
}).appendTo('#somewhere')
  .filter(function() {
      var id = this.getAttribute('data-pid');
      // return the element if the previous sibling has the same PID
      return $(this).prev('[data-pid="'+id+'"]').length;
  }).remove();

您在使用
排序功能时遇到了什么问题?非常感谢。。。我需要阅读appendTo(),但我认为这将覆盖那里的任何内容。。。非常感谢。。。