How do you sort the below object based on the value in
Well first you need to make an array of arrays:
#javascript #sort #alphabetically
Javascript
?{
"en": "English (English)",
"fa": "French",
"ar": "العربیه",
"zh": "中文 (Chinese)"
}
Well first you need to make an array of arrays:
var sortable_langs = [];
$.each(languages, function(index, language) {
sortable_langs.push([index, language]);
});
sortable_langs.sort(function(a, b) {
if(a[1] < b[1]) { return -1; }
if(a[1] > b[1]) { return 1; }
return 0;
});
NOTE:
a[1]
and b[1]
refers to the language value like: English (English). They are compared with each other. If a[1] is bigger than b[1] then it will be moved to the first to the top.#javascript #sort #alphabetically