我在网页中创建了一个过滤器框,Javascript 代码将数据从 json 调用到过滤器框中.如何按字母顺序排列数据



这段代码能够从搜索框中的表格中获取我的结果。我想要的只是让它给我按字母排序的结果。

 myarray.sort()

我尝试使用 myarray.sort(); 但它不会通过。

function buildLocationList(features) {
var listings = document.getElementById('listings');
listings.innerHTML = '';
 // Iterate through the list of stores
if(features.length > 0){
features.forEach(function(feature, i){
    var currentFeature = feature;
    // Shorten data.feature.properties to just `prop` so we're not
    // writing this long form over and over again.
    var prop = currentFeature.properties;
    // Select the listing container in the HTML and append a div
    // with the class 'item' for each store
    var listing = document.createElement('div')
    listing.className = 'item';
    listing.id = 'listings' + i;
// Create a new link with the class 'title' for each store
    // and fill it with the store address
    var link = document.createElement('a');
    link.href = '#';
    link.className = 'title';
    link.dataPosition = i;
    link.innerHTML = prop.Project_Name; 
});

不确定"它不会通过"是什么意思。 但是sort()函数的工作方式与您在此处的完全一样。

也许您希望它以不区分大小写的方式进行排序? 如果是这样,需要做更多的工作,但不会太多。

var months = ['March', 'Jan', 'Feb', 'Dec', 'jan', 'dec', 'feb', 'march'];
months.sort();
console.log("regular sort", months);
months.sort(function (a, b) {
    return a.toLowerCase().localeCompare(b.toLowerCase());
});
console.log("case insensitive sort",months);

最新更新