materials = {"Глина" : 10, "Цемент" : 5, "Вода" : 8, "Песок" : 14}
sort_arr = ["Песок", "Цемент", "Вода"]
sort = function(object, pattern){
var result={}
pattern.forEach(function(name){
result[name]=object[name]
})
Object.keys(object).forEach(function(name){if(pattern.indexOf(name) === -1) result[name] = object[name]})
return result
}
//alert(JSON.stringify(sort(materials, sort_arr)))
sort(materials, sort_arr)
alert(JSON.stringify(materials))
//>>>> {"Песок":14,"Цемент":5,"Вода":8,"Глина":10}
|