使用 JavaScript 合并和排序来自 SharePoint 的两个数据集合

Combining and sorting two collections of data from SharePoint using JavaScript

我是写作新手 javascript,我想按字母顺序打印出列表中的项目以及子站点。如何按字母顺序打印此组合输出?当前输出按 ABC 顺序显示列表项,然后按 ABC 顺序显示子站点。

$(document).ready(function() {
    GetItems();
});

function GetItems() {
    $().SPServices({
        operation: "GetListItems",
        async: true,
        listName: "GatheredSites",
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
        CAMLQuery: "<Query><OrderBy><FieldRef Name='Title' Ascending='True' /></OrderBy></Query>",
        completefunc: function(xData, Status) {
            if (Status == 'success') {
                $(xData.responseXML).SPFilterNode("z:row").each(function() {
                    var name = ($(this).attr("ows_Title"));
                    $("#divItems").append("<li>" + name + "</li>");
                });
                Subsites();
            } else {
                jQuery("#displayItems").append("<div>Empty</div>");
            }

        }
    });

}

function Subsites() {
    $().SPServices({
        operation: "GetWebCollection",
        webURL: "*url*",
        async: true,
        completefunc: function(xData, Status) {
            $(xData.responseXML).find("Webs > Web").each(function() {
                var $node = $(this);
                var liHtml = "<li> <a href='" + $node.attr("Url") + "' >" + $node.attr("Title") + "</a> </li>";
                $("#displayItems").append(liHtml);
            });

        }
    });
}
    < div id = "displayItems" >
    < /div>

您可以将所有列表和子站点名称保存到同一个数组中,然后在遍历它以打印结果之前对数组进行排序(使用 Array.sort())。

function GetItems() {
    var names = [];
    $().SPServices({
        operation: "GetListItems",
        async: true,
        listName: "GatheredSites",
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
        CAMLQuery: "<Query><OrderBy><FieldRef Name='Title' Ascending='True' /></OrderBy></Query>",
        completefunc: function (xData, Status) {
            if (Status == 'success') {
                $(xData.responseXML).SPFilterNode("z:row").each(function () {
                    var name = ($(this).attr("ows_Title"));
                    names.push(name);
                });
                $().SPServices({
                    operation: "GetWebCollection",
                    webURL: "*url*",
                    async: true,
                    completefunc: function (xData, Status) {
                        $(xData.responseXML).find("Webs > Web").each(function () {
                            var $node = $(this);
                            names.push($node.attr("Title"));
                        });
                        names.sort();
                        var output = $("#divItems");
                        for (var i = 0, len = names.length; i < len; i++) {
                            output.append("<li>" + names[i] + "</li>");
                        }
                    }
                });
            } else {
                jQuery("#displayItems").append("<div>Empty</div>");
            }    
        }
    });
}