如何按每个条目右侧的语言对此 JSON 对象进行排序?

发布于 2024-09-08 14:49:03 字数 1181 浏览 4 评论 0原文

我正在使用 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"
}

感谢你们的帮助:)

I am creating an Android app using PhoneGap and jqTouch, and I have a JSON object containing a word along with a language which that word is in. How can I sort this alphabetically, by the column on the right (the language), using Javascript? The end result should be Afrikaans as the first entry and Zulu as the last.

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"
}

Thanks for your help guys :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-09-15 14:49:03

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 does not guarantee any ordering of object properties. So you'll need to create an array which numerically indexes each entry.

E.g.:

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

Then, to sort by language, you can:

list = list.sort(function (a, b) { return a.lang < b.lang; });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文