我正在使用PhoneGap和jqTouch创建一个Android应用,并且我有一个JSON对象,其中包含一个单词以及该单词所在的语言。如何使用Javascript按右侧列(该语言)的字母顺序对其进行排序?最终结果应为南非荷兰语作为第一个条目,而祖鲁语作为最后一个条目。

var languages = {
    "Hello" :   "English",
    "Bonjour"   :   "French",
    "Hola"  :   "Spanish",
    "السّلام عليكم" :   "Arabic",
    "Haai"  :   "Afrikaans",
    "Nei ho"    :   "Cantonese".
    "Goddag"    :   "Danish",
    "Goede dag" :   "Dutch",
    "Saluton"   :   "Esperanto",
    "Hei"   :   "Finnish",
    "Guten tag" :   "German",
    "Gia'sou"   :   "Greek",
    "Aloha" :   "Hawaiian",
    "Hebrew"    :   "Shalom",
    "Namaste"   :   "Hindi",
    "Halo"  :   "Indonesian",
    "Aksunai"   :   "Inuit",
    "Dia dhuit" :   "Irish",
    "Salve" :   "Spanish",
    "Kon-nichiwa"   :   "Japanese",
    "An-nyong Ha-se-yo" :   "Korean",
    "Mandarin"  :   "Ni hao",
    "Hallo" :   "Norweigan",
    "Dzien' dobry"  :   "Polish",
    "Jambo" :   "Swahili",
    "Hej"   :   "Swedish",
    "Sa-wat-dee"    :   "Thai",
    "Merhaba"   :   "Turkish",
    "Vitayu"    :   "Ukrainian",
    "Hylo"  :   "Welsh",
    "Sholem aleychem"   :   "Yiddish",
    "Sawubona"  :   "Zulu"
}


谢谢您的帮助:)

最佳答案

JavaScript不保证对象属性的任何顺序。因此,您需要创建一个数组,该数组以数字方式索引每个条目。

例如。:

var list = [];
for (x in languages) {
  list.push({lang: languages[x], word: x});
}


然后,按语言排序,您可以:

list = list.sort(function (a, b) { return a.lang < b.lang; });

关于javascript - 如何按每个条目右侧的语言对该JSON对象进行排序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3175806/

10-09 15:57