/    Sign up×
Community /Pin to ProfileBookmark

Dictionary does not keep sorting???

I am trying to take a Dic and sort it in an array, sort it, and put it back into a Dic, but it will not keep the sort on the name? Can this be done, if so, how?

Here is my code:

[CODE]
var dict = {
12543: “Smith, David(12543)”,
48392: “Rogers, Jim(48392 )”,
26788: “Everton, Mike(26788)”
};

var arr = [];
for (var i in dict) {
arr[arr.length] = { val: i, name: dict[i] };
}

arr.sort(function (a, b) {
var nameA = a.name.toLowerCase(), nameB = b.name.toLowerCase()
if (nameA < nameB) //sort string ascending
return -1
if (nameA > nameB)
return 1
return 0 //default return value (no sorting)
})

dict = {};
for (var i = 0; i < arr.length; i++) {
var el = arr[i];
dict[el.val] = el.name;
}
return dict;
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@TcobbJun 14.2016 — You cannot really predict the order in which the keys of an object will "peel" off in something like a "for in" loop. If you want to sort the internal values they need to be transferred to an array in such a way that they can be referenced back to the appropriate key.
Copy linkTweet thisAlerts:
@rootJun 15.2016 — You need to try something simpler like this
var dict = {
12543: "Smith, David(12543)",
48392: "Rogers, Jim(48392 )",
26788: "Everton, Mike(26788)"
};
// sort the elements in to a new array
var tmp = [];
for(d in dict){
tmp.push( { ext:d,
surname:dict[d].slice(0,x=dict[d].indexOf(", ")),
firstname:dict[d].slice(x+1,dict[d].indexOf("(",x))
});
}
tmp.sort(function (a, b) { return a.surname &gt; b.surname });
for(t=0; t&lt;tmp.length; t++) console.log("&gt; surname : " + tmp[t].surname + " -- " +tmp[t].firstname + " -- ext : " + tmp[t].ext )


Hit F12 to get the browser console up

the output
&gt; surname : Everton -- Mike -- ext : 26788
<i>&gt; </i>surname : Rogers -- Jim -- ext : 48392
<i>&gt; </i>surname : Smith -- David -- ext : 12543
Copy linkTweet thisAlerts:
@daveyerwinJun 15.2016 — You cannot really predict the order in which the keys of an object will "peel" off in something like a "for in" loop. If you want to sort the internal values they need to be transferred to an array in such a way that they can be referenced back to the appropriate key.[/QUOTE]

so taking your advice ...
[CODE]

<script>

var dict = {
12543: "Smith, David(12543)",
48392: "Rogers, Jim(48392 )",
26788: "Everton, Mike(26788)"
};
b=[];
for(a in dict)
b.push(dict[a]);
b.sort();
for(i=0;i<b.length;i++)
b[i]=parseInt(b[i].split('(')[1]);
dict.order=b;
dict.dsplay=function(){
for(i=0;i<this.order.length;i++)
alert(this[this.order[i]])
};
dict.dsplay();
</script>

[/CODE]
×

Success!

Help @helixpoint spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...