-2

For those that already voted(negative), i show what i needed!

var obj = {
    item_1: {name:'aaa',weight:4},
    item_2: {name:'ddd',weight:2},
    item_5: {name:'eee',weight:0},
    item_3: {name:'ccc',weight:3},
    item_6: {name:'ccc',weight:23},
    item_4: {name:'eee',weight:1},
}

var arr = _.toPairs(obj)

console.log(arr)

var sortedArr = arr.sort(function(a,b){ return b[1].weight - a[1].weight})

console.log(sortedArr)

var sortedObj = _.fromPairs(sortedArr)

console.log(JSON.stringify(sortedObj))

live link here: sort Object based on 'weight'property

please study before you judge.

i have an object array like this:

var obj = {
    item_1: {name:'aaa',weight:4},
    item_2: {name:'ddd',weight:2},
    item_3: {name:'ccc',weight:3},
    item_4: {name:'eee',weight:1},
}

When i run: _.orderBy or _.sortBy()

e.g. : _.orderBy(obj,['weight']) .

i get the sorted array , but without the initial keys

0: {name: "eee", weight: 1}
1: {name: "ddd", weight: 2}
2: {name: "ccc", weight: 3}
3: {name: "aaa", weight: 4}

But i need the original keys item_1, item_2 etc.

Can anyone give a hand ? Thanks.

Theo Itzaris
  • 4,321
  • 3
  • 37
  • 68
  • You can't have sorted object. Objects are inherently not sorted in javascript. If you want to keep original keys you could save them inside inner object as a property. – Yury Tarabanko Aug 22 '17 at 10:06
  • How do you want the initial keys to be in the new array? And how do you want the new array to be sorted, by weight, by name, by the keys?? – kevguy Aug 22 '17 at 10:09
  • instead of replacing the keys with 0,1,2,3 kust keep the item_1, item_2,item_3 & item_4 – Theo Itzaris Aug 22 '17 at 10:10
  • @TheoItzaris It is not "replacing keys with 0, 1" it creates new array because you can't have a plain object with sorted properties. The order of keys is not guaranteed by spec. So you shouldn't rely on it. – Yury Tarabanko Aug 22 '17 at 10:18
  • @YuryTarabanko is right, you cannot have sorted properties inside an object. Read this for more info: https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – kevguy Aug 22 '17 at 11:27

1 Answers1

0

See if this will help you. You may delete the extra key property if needed.

var obj = {
    item_1: {name:'aaa',weight:4},
    item_2: {name:'ddd',weight:2},
    item_5: {name:'eee',weight:0},
    item_3: {name:'ccc',weight:3},
    item_6: {name:'ccc',weight:23},
    item_4: {name:'eee',weight:1},
}

function buildItem(item, key) {
    var newItem = { key: key };
    newItem[key] = item;
    return newItem;
}

function byWeight(item) {
    return item[item.key].weight;
}

var arr = _(obj).map(buildItem).sortBy(byWeight).value();
console.log(arr);
Rodris
  • 2,603
  • 1
  • 17
  • 25
  • No man, i achieved what i need. 1. get the object. 2. convert it to array 3. sort it 4. convert it back to an object with sorted properties. I return object at the end, and that was the hard part: console.log(sortedObj) You return an array. – Theo Itzaris Aug 23 '17 at 06:25