Sorting object in javascript -
this question has answer here:
- sorting object sub-object property 3 answers
i have object has object value. want sort object based on 'copy' key value. fiddle
var x= {'one':{'copy':'b'},'two':{'copy':'v'},'three':{'copy':'a'}} var getsort= [] for(i in x){ var a= new object(); a[i]=x[i] getsort.push(a) } getsort.sort(function(a,b){ //console.log(b) //console.log(a.copy) var texta = a.copy.touppercase(); var textb = b.copy.touppercase(); return (texta < textb) ? -1 : (texta > textb) ? 1 : 0; }) console.log(getsort)
this work you:
var x= {'one':{'copy':'b'},'two':{'copy':'v'},'three':{'copy':'a'}} var getsort= [] for(i in x){ if (i != undefined) { //console.log(i); var a= new object(); a[i]=x[i] //console.log(a[i]); getsort.push(a); } } function getvalue(data) { for(key in data) { if(data.hasownproperty(key)) { var value = data[key]; return value; } } } getsort.sort(function(a,b){ console.log(getvalue(b).copy) console.log(getvalue(a).copy) var texta = getvalue(a).copy.touppercase(); var textb = getvalue(b).copy.touppercase(); return (texta < textb) ? -1 : (texta > textb) ? 1 : 0; }) console.log(getsort);
Comments
Post a Comment