首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从数组中填充的排序顺序对html进行排序

从数组中填充的排序顺序对html进行排序
EN

Stack Overflow用户
提问于 2016-10-18 16:21:44
回答 1查看 113关注 0票数 1

我正在尝试按距离对html列表进行排序,我不想将距离属性放入我的标记中。I do to put distance property in my markup。我所做的是渲染商店列表html,在js中我创建了一个保存商店和距离的数组。

代码语言:javascript
复制
locations = [];
locations.push({
    'store': stores[i],
    'cordinate': Math.random()
});

我用这个对它进行排序

代码语言:javascript
复制
locations.sort(dynamicSort('cordinate'));

function dynamicSort(property) {
  var sortOrder = 1;

  return function(a, b) {
    var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
    return result * sortOrder;
  }
}

这就是我被卡住的地方,我如何使用这个数组对html进行排序?这里的关键是,我不想因为在那里设置坐标而弄乱我的html。

My fiddle is right here

编辑

这个问题是关于使用数组中填充的排序顺序对输出html进行排序。在那里,我删除了html和其他js,我只是因为懒于填充标记而使用它们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-18 16:42:36

基本上,您需要做的就是按照排序顺序在html元素上调用append

这会将元素附加到html元素的末尾。

代码语言:javascript
复制
var sortHTML = function(locations) {
  $.each(locations, function(i, location) {
    var targetElement = $('#' + location.store.id);
    targetElement.parent().append(targetElement)
  });
};
sortHTML(locations);

小提琴在这里:https://jsfiddle.net/wbcj026x/1/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40103123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档