2

I am trying to sort object of objects obj in javascript on the basis of cummulativeSite in descending order through object.keys but it is not getting sorted.Please suggest a good approach or a method for the same to sort this.

var obj={


 B.E:{
    A: {percentgsite: 8.5, backlogenclsite: 8.5, regularenclsite: 3.5, cummulativeSite: 20.5},
    B: {percentgsite: 5.9, backlogenclsite: 5.9, regularenclsite: 8.5, cummulativeSite: 20.3},
    C: {percentgsite: 5.9, backlogenclsite: 6.4, regularenclsite: 8.9, cummulativeSite: 21.200000000000003}
    },



 Diploma:{
    A: {percentgsite: 8.5, cummulativeSite: 5.2},
    B: {percentgsite: 5.9, cummulativeSite: 8.5},
    C: {percentgsite: 5.9, cummulativeSite: 5.6}
    },



 M.E:{
    A: {percentgsite: 8.5, cummulativeSite: 11.9},
    B: {percentgsite: 5.9, cummulativeSite: 12.9},
    C: {percentgsite: 5.9, cummulativeSite: 13.5}
    }
}

Expected Output :

var obj={


 B.E:{

    C: {percentgsite: 5.9, backlogenclsite: 6.4, regularenclsite: 8.9, cummulativeSite: 21.200000000000003},
    A: {percentgsite: 8.5, backlogenclsite: 8.5, regularenclsite: 3.5, cummulativeSite: 20.5},
    B: {percentgsite: 5.9, backlogenclsite: 5.9, regularenclsite: 8.5, cummulativeSite: 20.3}
    },



 Diploma:{
    B: {percentgsite: 8.5, cummulativeSite: 8.5},
    C: {percentgsite: 5.9, cummulativeSite: 5.6},
    A: {percentgsite: 5.9, cummulativeSite: 5.2}
    },



 M.E:{
    C: {percentgsite: 8.5, cummulativeSite: 13.5},
    B: {percentgsite: 5.9, cummulativeSite: 12.9},
    A: {percentgsite: 5.9, cummulativeSite: 11.9}
    }
}
8
  • 2
    That's not a valid array. Is arr an array or an object with B.E, Diploma and M.E as keys? Commented Feb 1, 2019 at 11:07
  • I suppose B.E, Diploma and M.E are independent arrays, am I correct? Commented Feb 1, 2019 at 11:08
  • @adiga it's an object Commented Feb 1, 2019 at 11:13
  • @WagnerdeAndradePerin they are objects Commented Feb 1, 2019 at 11:14
  • 1
    @Kris Object don't guaranty order, so you B.E, M.E needs to be array if you want order Commented Feb 1, 2019 at 11:14

2 Answers 2

1

As you mentioned in comments you're ok to change B.E and so on to array ( Because object doesn't guaranty order ). So i will change them in array and than sort them based on cummulativeSite property

var obj={'B.E':[{A:{percentgsite:8.5,backlogenclsite:8.5,regularenclsite:3.5,cummulativeSite:20.5}},{B:{percentgsite:5.9,backlogenclsite:5.9,regularenclsite:8.5,cummulativeSite:20.3}},{C:{percentgsite:5.9,backlogenclsite:6.4,regularenclsite:8.9,cummulativeSite:21.200000000000003}}],Diploma:[{A:{percentgsite:8.5,cummulativeSite:5.2}},{B:{percentgsite:5.9,cummulativeSite:8.5}},{C:{percentgsite:5.9,cummulativeSite:5.6}}],'M.E':[{A:{percentgsite:8.5,cummulativeSite:11.9}},{B:{percentgsite:5.9,cummulativeSite:12.9}},{C:{percentgsite:5.9,cummulativeSite:13.5}}]}

Object.keys(obj).forEach(e => {
  obj[e].sort( (a,b) =>{
   return Object.values(a)[0].cummulativeSite -
   Object.values(b)[0].cummulativeSite
   })
})

console.log(obj)

Sign up to request clarification or add additional context in comments.

3 Comments

var obj={ B.E:[ 0:{ A: {percentgsite: 8.5, backlogenclsite: 8.5, regularenclsite: 3.5, cummulativeSite: 20.5}, B: {percentgsite: 5.9, backlogenclsite: 5.9, regularenclsite: 8.5, cummulativeSite: 20.3}; C: {percentgsite: 5.9, backlogenclsite: 6.4, regularenclsite: 8.9, cummulativeSite: 21.200000000000003} } ], @Code Maniac i am getting my B.E array in this format. Can you please suggest sorting now
@Kris your posted one doesn't seems to be valid one { B.E: [ 0:
this 0 is the index at which array is present @Code Maniac
0

You could use a nested Object.entries and reduce like this:

var obj={"B.E":{A:{percentgsite:8.5,backlogenclsite:8.5,regularenclsite:3.5,cummulativeSite:20.5},B:{percentgsite:5.9,backlogenclsite:5.9,regularenclsite:8.5,cummulativeSite:20.3},C:{percentgsite:5.9,backlogenclsite:6.4,regularenclsite:8.9,cummulativeSite:21.200000000000003}},"Diploma":{A:{percentgsite:8.5,cummulativeSite:5.2},B:{percentgsite:5.9,cummulativeSite:8.5},C:{percentgsite:5.9,cummulativeSite:5.6}},"M.E":{A:{percentgsite:8.5,cummulativeSite:11.9},B:{percentgsite:5.9,cummulativeSite:12.9},C:{percentgsite:5.9,cummulativeSite:13.5}}}

const sortedObj = Object.entries(obj).reduce((acc, [dept, v]) =>{
  const sortedDept = Object.entries(v)
             .sort(([k1,v1], [k2,v2]) => v2.cummulativeSite - v1.cummulativeSite)
             .reduce((r, [k,v]) => (r[k] = v, r), {});
  
  acc[dept] = sortedDept;
  return acc;
}, {})

console.log(sortedObj)

Forgot to mention that, properties order in objects is not guaranteed in JavaScript, especially if you have integer keys. If you decide to change the inner items to an array, you get the idea.

4 Comments

if i take B.E, Diploma and M.E as an array inside this object will that suffice @adiga
@Kris how are you planning on converting "B.E" to an array? Will the structure be "B.E": [ { "A": { percentgsite } }, { "B": { percentgsite } } ] OR "B.E": [ { percentgsite}, { percentgsite} ]
this is the format when i change it into array can i implement the way you suggested to get the sorting done in this case.. @adiga
var obj={ B.E:[ 0:{ A: {percentgsite: 8.5, backlogenclsite: 8.5, regularenclsite: 3.5, cummulativeSite: 20.5}, B: {percentgsite: 5.9, backlogenclsite: 5.9, regularenclsite: 8.5, cummulativeSite: 20.3}; C: {percentgsite: 5.9, backlogenclsite: 6.4, regularenclsite: 8.9, cummulativeSite: 21.200000000000003} } ], @adiga

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.