如何在 JavaScript 中对 NodeList 进行重新排序/排序?

发布于 2024-10-13 09:23:27 字数 749 浏览 3 评论 0原文

我有一个我认为应该是一个简单的问题;让我快速解释一下:

在我的 JavaScript 中,food.xml 是用以下方式读取的:

getMenuXml.open("GET","food.xml",false);
getMenuXml.send();
xmlDoc=getMenuXml.responseXML;
xmlFoodList = xmlDoc.getElementsByTagName("food");

所以现在我有一个包含所有食物元素的 NodeList xmlFoodList。到目前为止很棒。问题是我想根据内部元素 对节点进行排序。我可以这样读:

xmlFoodList[i].getElementsByTagName("category")[0].childNodes[0].nodeValue

稍后在我的代码中,食物项目显示在列表中,正如您所期望的,我希望将同一类别的食物列出在一起。所以,我的问题是:如何根据类别对 xmlFoodList 中的节点重新排序?

注意:我无法更改 food.xml进来,我不想编辑稍后的代码来在填充列表时进行排序。我不想将 NodeList 转换为数组,因为我必须重写很多后续代码。性能实际上并不是什么大问题,因此您可以随意克隆/嵌套循环。感谢您抽出时间。

I have what I think should be a straightforward question; let me quickly explain:

In my JavaScript, food.xml is read in with:

getMenuXml.open("GET","food.xml",false);
getMenuXml.send();
xmlDoc=getMenuXml.responseXML;
xmlFoodList = xmlDoc.getElementsByTagName("food");

so that now I have a NodeList xmlFoodList with all the food elements. Great so far. The problem is I want to sort the nodes based on an element <category> inside. I can read that with:

xmlFoodList[i].getElementsByTagName("category")[0].childNodes[0].nodeValue

Later in my code, the food items are displayed in a list, and as you'd expect, I want food of the same category to be listed together. So, my question is: How can I reorder the nodes in xmlFoodList based on their category?

Notes: I can't change food.xml coming in, and I don't want to edit my later code to do the sorting as the list is populated. I don't want to convert the NodeList to an array, since I'd have to rewrite a lot of later code. Performance isn't actually much of a concern, so feel free to clone/nested loop all you want. Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皇甫轩 2024-10-20 09:23:27

如果您首先将 NodeList 的元素转换为数组,则可以对它们进行排序:

var foods = xmlDoc.getElementsByTagName("food");
var foodsArray = Array.prototype.slice.call(foods, 0);

然后您可以使用 sort 方法:

foodsArray.sort(function(a,b) {
    var aCat = a.getElementsByTagName("category")[0].childNodes[0].nodeValue;
    var bCat = b.getElementsByTagName("category")[0].childNodes[0].nodeValue;
    if (aCat > bCat) return 1;
    if (aCat < bCat) return -1;
    return 0;
});

但这高度依赖于您的 XML 模式 - 例如,如果您有属于多个类别的食物只会按上面代码中的第一个类别进行排序。

You can order the elements of the NodeList, if you convert them to an array first:

var foods = xmlDoc.getElementsByTagName("food");
var foodsArray = Array.prototype.slice.call(foods, 0);

Then you can use the sort method:

foodsArray.sort(function(a,b) {
    var aCat = a.getElementsByTagName("category")[0].childNodes[0].nodeValue;
    var bCat = b.getElementsByTagName("category")[0].childNodes[0].nodeValue;
    if (aCat > bCat) return 1;
    if (aCat < bCat) return -1;
    return 0;
});

This is highly dependent on your XML schema though - if, for example, you had foods which were in more than one category they would only be sorted by the first category in the code above.

心碎无痕… 2024-10-20 09:23:27

看看这个:Xml、xsl Javascript 排序。最坏的情况是,您将数据转换为完全相同的 xml,但已排序。只要您支付转换惩罚,您就可以考虑将其转换为对下一步更有用的形式。

Take a look at this: Xml, xsl Javascript sorting. Worst case scenario, you transform the data into precisely the same xml, but sorted. As long as you're paying the transformation penalty, you might consider transforming it into a form more useful for whatever the next step is.

乜一 2024-10-20 09:23:27

使用 Javascript 库来获取节点列表操作的现成函数是一个好主意,例如重新排序、排序、foreach 等。我推荐 YUI-3,请参阅 YUI-3 NodeList

It's a good idea to use a Javascript library to get ready-made functions w.r.t Node List operations such as re-ordering, sorting, foreach etc. I would recommend YUI-3, please refer YUI-3 NodeList .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文