2016-09-24 74 views
1

我的數組對象如下JavaScript的數組對象排序工作不正常

var data = [{"weight":0,"name":"New Arrivals"},{"weight":0,"name":"Weekly Promotions"},{"weight":0,"name":"Sale"},{"weight":0,"name":"Extended Size"},{"weight":0,"name":"Accessories and Shoes"},{"weight":0,"name":"Activewear"},{"weight":0,"name":"Disney Project"},{"weight":0,"name":"Dresses and Jumpsuits"},{"weight":0,"name":"Fleece"},{"weight":0,"name":"HEATTECH Collection"},{"weight":0,"name":"Ines de la Fressange"},{"weight":0,"name":"Intimates"},{"weight":0,"name":"Jeans"},{"weight":0,"name":"Loungewear"},{"weight":0,"name":"Outerwear and Blazers"},{"weight":0,"name":"Pants"},{"weight":0,"name":"Shirts and Blouses"},{"weight":0,"name":"Skirts"},{"weight":0,"name":"Socks and Hosiery"},{"weight":0,"name":"Sweaters"},{"weight":0,"name":"Sweatshirts and Sweatpants"},{"weight":0,"name":"T-Shirts and Tops"},{"weight":0,"name":"UT: Graphic Tees"},{"weight":0,"name":"Wear To Work"},{"weight":0,"name":"Mix and Match"}] 

正嘗試根據性質權重排序這一點,但在情況下,如果重量的所有值是0,我需要的原因爲它在輸出上。但是這個功能並沒有像預期的那樣工作,而是順序在變化。

曾用ramda以及JavaScript的排序

var sortedData = R.sortBy(R.prop('weight'), data); 

var sortData = data.slice(0); 
sortData.sort(function(a,b) { 
    return a.weight - b.weight; 
}); 

兩種情況下我得到的結果如下

var output = [{"weight":0,"name":"Jeans"},{"weight":0,"name":"New Arrivals"},{"weight":0,"name":"Sale"},{"weight":0,"name":"Extended Size"},{"weight":0,"name":"Accessories and Shoes"},{"weight":0,"name":"Activewear"},{"weight":0,"name":"Disney Project"},{"weight":0,"name":"Dresses and Jumpsuits"},{"weight":0,"name":"Fleece"},{"weight":0,"name":"HEATTECH Collection"},{"weight":0,"name":"Ines de la Fressange"},{"weight":0,"name":"Intimates"},{"weight":0,"name":"Weekly Promotions"},{"weight":0,"name":"Loungewear"},{"weight":0,"name":"Outerwear and Blazers"},{"weight":0,"name":"Pants"},{"weight":0,"name":"Shirts and Blouses"},{"weight":0,"name":"Skirts"},{"weight":0,"name":"Socks and Hosiery"},{"weight":0,"name":"Sweaters"},{"weight":0,"name":"Sweatshirts and Sweatpants"},{"weight":0,"name":"T-Shirts and Tops"},{"weight":0,"name":"UT: Graphic Tees"},{"weight":0,"name":"Wear To Work"},{"weight":0,"name":"Mix and Match"}] 

預期:當體重值是0所有的情況下,那麼我期待的結果與輸入一樣。

任何幫助表示讚賞。

回答

1

最簡單的解決方法是保存的位置,然後進行比較,如果對象是相等的:

var sortData = data.slice(0); 

sortData.forEach(function(element, index){ 
    element.index = index; 
}); 

sortData.sort(function(a,b) { 
    var diff = a.weight - b.weight; 
    return diff === 0 ? a.index - b.index : diff; 
}); 

sortData.forEach(function(element){ 
    delete element.index; 
}); 
1

你所尋找的是叫stability排序算法的性能。這意味着排序後,等值元素的順序保持不變。不同的瀏覽器使用不同的算法進行排序,其中一些瀏覽器使用穩定的算法,一些使用不穩定的算法。你可以看到這個SO線程上的瀏覽器之間的區別:https://stackoverflow.com/a/3027715/1641070

正如你可以在Ramda來源看,它使用內置的排序機制,讓您得到相同的結果,如果你使用Ramda:https://github.com/ramda/ramda/blob/v0.22.1/src/sortBy.js#L38:L42

1

一個建議與Sorting with map。在這裏,您有索引,並使用第二個鏈接的排序標準保留原始排序。

mapped.sort(function (a, b) { 
    return a.value - b.value || a.index - b.index; 
}); 

// the array to be sorted 
 
var data = [{ weight: 0, name: "New Arrivals" }, { weight: 0, name: "Weekly Promotions" }, { weight: 0, name: "Sale" }, { weight: 0, name: "Extended Size" }, { weight: 0, name: "Accessories and Shoes" }, { weight: 0, name: "Activewear" }, { weight: 0, name: "Disney Project" }, { weight: 0, name: "Dresses and Jumpsuits" }, { weight: 0, name: "Fleece" }, { weight: 0, name: "HEATTECH Collection" }, { weight: 0, name: "Ines de la Fressange" }, { weight: 0, name: "Intimates" }, { weight: 0, name: "Jeans" }, { weight: 0, name: "Loungewear" }, { weight: 0, name: "Outerwear and Blazers" }, { weight: 0, name: "Pants" }, { weight: 0, name: "Shirts and Blouses" }, { weight: 0, name: "Skirts" }, { weight: 0, name: "Socks and Hosiery" }, { weight: 0, name: "Sweaters" }, { weight: 0, name: "Sweatshirts and Sweatpants" }, { weight: 0, name: "T-Shirts and Tops" }, { weight: 0, name: "UT: Graphic Tees" }, { weight: 0, name: "Wear To Work" }, { weight: 0, name: "Mix and Match" }]; 
 

 
// temporary array holds objects with position and sort-value 
 
var mapped = data.map(function (el, i) { 
 
     return { 
 
      index: i, 
 
      value: el.weight 
 
     }; 
 
    }); 
 

 
// sorting the mapped array containing the reduced values 
 
mapped.sort(function (a, b) { 
 
    return a.value - b.value || a.index - b.index; 
 
}); 
 

 
// container for the resulting order 
 
var result = mapped.map(function (el) { 
 
    return data[el.index]; 
 
}); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

您可以使用下劃線的_.sortBy穩定排序(數據, '重量')。 check this