0

This is my original JavaScripts Object

novels = {
    "39912": "Conan's gramology investigator(柯南里的克学调查员)",
    "40519": "Simulator: I predict the future in the last days(模拟器:我在末世预测未来)",
    "40859": "Reborn Football Simulator(重生之足球模拟器)",
    "41052": "Naruto comes to Marvel(降临漫威的火影忍者)",
    "43681": "Uchiha's Life Simulator(宇智波的人生模拟器)",
    "46128": "Life simulator, start with a maid(人生模拟器,开局一宫女)",
    "9961541": "Konoha Gatekeeper I was overheard by Tsunade(木叶守门人我被纲手偷听了心声)",
    "9982243": "King: My ultimate hit rate is 100%(王者:我的大招命中率百分百)",
    "10061579": "Hokage: Tsunade is pregnant, I can only become stronger!(火影:纲手怀孕了,我只能变强!)",
    "10064100": "Naruto: God-level sign-in starts with unlimited chakra(火影:神级签到从无限查克拉开始)",
    "10064164": "Pokémon: Sign in at the beginning of the Koga Ninja Frog(神奇宝贝:开局签到甲贺忍蛙)",
    "10064304": "Genocide Night, fortunately I have a ninja simulator(灭族之夜,还好我有忍者模拟器)",
    "10064399": "Naruto: I can reach full level with one click(火影:我能一键满级)",
    "10064576": "Naruto: My clone can evolve(火影:我的分身能进化)",
    "10064579": "People in Konoha, start life simulator(人在木叶,开局人生模拟器)",
    "10064599": "Naruto: Uchiha, the salted fish, was eavesdropped by Itachi(火影:咸鱼宇智波,被鼬偷听心声)",
    "10064824": "The salted fish ninja was overheard by Orochimaru(咸鱼忍者,被大蛇丸偷听心声)",
    "10064894": "Naruto: Sign in to Reaper's Scythe at the beginning, harvest wildly(火影:开局签到死神之镰,疯狂收割)",
    "10065466": "Pirates: Sign in at the beginning of the Naruto character card(海贼:开局签到火影角色卡)",
    "10072166": "Voice of Journey to the West: Beginning Guide Monkey King(西游心声:开局指引孙悟空)"
};

I want to sort it by value like this (i used notepad to make it)

novels = {
    "39912": "Conan's gramology investigator(柯南里的克学调查员)"
    "10064304": "Genocide Night, fortunately I have a ninja simulator(灭族之夜,还好我有忍者模拟器)"
    "10061579": "Hokage: Tsunade is pregnant, I can only become stronger!(火影:纲手怀孕了,我只能变强!)"
    "9982243": "King: My ultimate hit rate is 100%(王者:我的大招命中率百分百)"
    "9961541": "Konoha Gatekeeper I was overheard by Tsunade(木叶守门人我被纲手偷听了心声)"
    "46128": "Life simulator, start with a maid(人生模拟器,开局一宫女)"
    "41052": "Naruto comes to Marvel(降临漫威的火影忍者)"
    "10064100": "Naruto: God-level sign-in starts with unlimited chakra(火影:神级签到从无限查克拉开始)"
    "10064399": "Naruto: I can reach full level with one click(火影:我能一键满级)"
    "10064576": "Naruto: My clone can evolve(火影:我的分身能进化)"
    "10064894": "Naruto: Sign in to Reaper's Scythe at the beginning, harvest wildly(火影:开局签到死神之镰,疯狂收割)"
    "10064599": "Naruto: Uchiha, the salted fish, was eavesdropped by Itachi(火影:咸鱼宇智波,被鼬偷听心声)"
    "10064579": "People in Konoha, start life simulator(人在木叶,开局人生模拟器)"
    "10065466": "Pirates: Sign in at the beginning of the Naruto character card(海贼:开局签到火影角色卡)"
    "10064164": "Pokémon: Sign in at the beginning of the Koga Ninja Frog(神奇宝贝:开局签到甲贺忍蛙)"
    "40859": "Reborn Football Simulator(重生之足球模拟器)"
    "40519": "Simulator: I predict the future in the last days(模拟器:我在末世预测未来)"
    "10064824": "The salted fish ninja was overheard by Orochimaru(咸鱼忍者,被大蛇丸偷听心声)"
    "43681": "Uchiha's Life Simulator(宇智波的人生模拟器)"
    "10072166": "Voice of Journey to the West: Beginning Guide Monkey King(西游心声:开局指引孙悟空)"
}

i found this Sorting object property by values and but didn't work for me

i tried this

var sortarr = Object.entries(novels).sort((a,b) => (a[1]>b[1]?1:-1));

it become arrays and it sort well

[
    [
        "39912",
        "Conan's gramology investigator(柯南里的克学调查员)"
    ],
    [
        "10064304",
        "Genocide Night, fortunately I have a ninja simulator(灭族之夜,还好我有忍者模拟器)"
    ],
    [
        "10061579",
        "Hokage: Tsunade is pregnant, I can only become stronger!(火影:纲手怀孕了,我只能变强!)"
    ],
    [
        "9982243",
        "King: My ultimate hit rate is 100%(王者:我的大招命中率百分百)"
    ],
    [
        "9961541",
        "Konoha Gatekeeper I was overheard by Tsunade(木叶守门人我被纲手偷听了心声)"
    ],
    [
        "46128",
        "Life simulator, start with a maid(人生模拟器,开局一宫女)"
    ],
    [
        "41052",
        "Naruto comes to Marvel(降临漫威的火影忍者)"
    ],
    [
        "10064100",
        "Naruto: God-level sign-in starts with unlimited chakra(火影:神级签到从无限查克拉开始)"
    ],
    [
        "10064399",
        "Naruto: I can reach full level with one click(火影:我能一键满级)"
    ],
    [
        "10064576",
        "Naruto: My clone can evolve(火影:我的分身能进化)"
    ],
    [
        "10064894",
        "Naruto: Sign in to Reaper's Scythe at the beginning, harvest wildly(火影:开局签到死神之镰,疯狂收割)"
    ],
    [
        "10064599",
        "Naruto: Uchiha, the salted fish, was eavesdropped by Itachi(火影:咸鱼宇智波,被鼬偷听心声)"
    ],
    [
        "10064579",
        "People in Konoha, start life simulator(人在木叶,开局人生模拟器)"
    ],
    [
        "10065466",
        "Pirates: Sign in at the beginning of the Naruto character card(海贼:开局签到火影角色卡)"
    ],
    [
        "10064164",
        "Pokémon: Sign in at the beginning of the Koga Ninja Frog(神奇宝贝:开局签到甲贺忍蛙)"
    ],
    [
        "40859",
        "Reborn Football Simulator(重生之足球模拟器)"
    ],
    [
        "40519",
        "Simulator: I predict the future in the last days(模拟器:我在末世预测未来)"
    ],
    [
        "10064824",
        "The salted fish ninja was overheard by Orochimaru(咸鱼忍者,被大蛇丸偷听心声)"
    ],
    [
        "43681",
        "Uchiha's Life Simulator(宇智波的人生模拟器)"
    ],
    [
        "10072166",
        "Voice of Journey to the West: Beginning Guide Monkey King(西游心声:开局指引孙悟空)"
    ]
]

but when i try to make object with

sortobj = Object.fromEntries(sortarr);

it become unsorted like original (sort by key) instead of sort by value

novels = {
    "39912": "Conan's gramology investigator(柯南里的克学调查员)",
    "40519": "Simulator: I predict the future in the last days(模拟器:我在末世预测未来)",
    "40859": "Reborn Football Simulator(重生之足球模拟器)",
    "41052": "Naruto comes to Marvel(降临漫威的火影忍者)",
    "43681": "Uchiha's Life Simulator(宇智波的人生模拟器)",
    "46128": "Life simulator, start with a maid(人生模拟器,开局一宫女)",
    "9961541": "Konoha Gatekeeper I was overheard by Tsunade(木叶守门人我被纲手偷听了心声)",
    "9982243": "King: My ultimate hit rate is 100%(王者:我的大招命中率百分百)",
    "10061579": "Hokage: Tsunade is pregnant, I can only become stronger!(火影:纲手怀孕了,我只能变强!)",
    "10064100": "Naruto: God-level sign-in starts with unlimited chakra(火影:神级签到从无限查克拉开始)",
    "10064164": "Pokémon: Sign in at the beginning of the Koga Ninja Frog(神奇宝贝:开局签到甲贺忍蛙)",
    "10064304": "Genocide Night, fortunately I have a ninja simulator(灭族之夜,还好我有忍者模拟器)",
    "10064399": "Naruto: I can reach full level with one click(火影:我能一键满级)",
    "10064576": "Naruto: My clone can evolve(火影:我的分身能进化)",
    "10064579": "People in Konoha, start life simulator(人在木叶,开局人生模拟器)",
    "10064599": "Naruto: Uchiha, the salted fish, was eavesdropped by Itachi(火影:咸鱼宇智波,被鼬偷听心声)",
    "10064824": "The salted fish ninja was overheard by Orochimaru(咸鱼忍者,被大蛇丸偷听心声)",
    "10064894": "Naruto: Sign in to Reaper's Scythe at the beginning, harvest wildly(火影:开局签到死神之镰,疯狂收割)",
    "10065466": "Pirates: Sign in at the beginning of the Naruto character card(海贼:开局签到火影角色卡)",
    "10072166": "Voice of Journey to the West: Beginning Guide Monkey King(西游心声:开局指引孙悟空)"
};

i also try this

var arr=[];
xc=Object.entries(novels).sort((a,b) => (a[1]>b[1]?1:-1));
for (var prop in xc) {
        if (xc.hasOwnProperty(prop)) {
            arr.push({
                [xc[prop][0]]: xc[prop][1]
            });
        }
    }

It become many object in one array

arr =[
    {
        "39912": "Conan's gramology investigator(柯南里的克学调查员)"
    },
    {
        "10064304": "Genocide Night, fortunately I have a ninja simulator(灭族之夜,还好我有忍者模拟器)"
    },
    {
        "10061579": "Hokage: Tsunade is pregnant, I can only become stronger!(火影:纲手怀孕了,我只能变强!)"
    },
    {
        "9982243": "King: My ultimate hit rate is 100%(王者:我的大招命中率百分百)"
    },
    {
        "9961541": "Konoha Gatekeeper I was overheard by Tsunade(木叶守门人我被纲手偷听了心声)"
    },
    {
        "46128": "Life simulator, start with a maid(人生模拟器,开局一宫女)"
    },
    {
        "41052": "Naruto comes to Marvel(降临漫威的火影忍者)"
    },
    {
        "10064100": "Naruto: God-level sign-in starts with unlimited chakra(火影:神级签到从无限查克拉开始)"
    },
    {
        "10064399": "Naruto: I can reach full level with one click(火影:我能一键满级)"
    },
    {
        "10064576": "Naruto: My clone can evolve(火影:我的分身能进化)"
    },
    {
        "10064894": "Naruto: Sign in to Reaper's Scythe at the beginning, harvest wildly(火影:开局签到死神之镰,疯狂收割)"
    },
    {
        "10064599": "Naruto: Uchiha, the salted fish, was eavesdropped by Itachi(火影:咸鱼宇智波,被鼬偷听心声)"
    },
    {
        "10064579": "People in Konoha, start life simulator(人在木叶,开局人生模拟器)"
    },
    {
        "10065466": "Pirates: Sign in at the beginning of the Naruto character card(海贼:开局签到火影角色卡)"
    },
    {
        "10064164": "Pokémon: Sign in at the beginning of the Koga Ninja Frog(神奇宝贝:开局签到甲贺忍蛙)"
    },
    {
        "40859": "Reborn Football Simulator(重生之足球模拟器)"
    },
    {
        "40519": "Simulator: I predict the future in the last days(模拟器:我在末世预测未来)"
    },
    {
        "10064824": "The salted fish ninja was overheard by Orochimaru(咸鱼忍者,被大蛇丸偷听心声)"
    },
    {
        "43681": "Uchiha's Life Simulator(宇智波的人生模拟器)"
    },
    {
        "10072166": "Voice of Journey to the West: Beginning Guide Monkey King(西游心声:开局指引孙悟空)"
    }
]

i try merge it to become object with this

numn = {...arr[0],...arr[1],...arr[2],...arr[3],...arr[4],...arr[5],...arr[6],...arr[7],...arr[8],...arr[9],...arr[10],...arr[11],...arr[12],...arr[13],...arr[14],...arr[15],...arr[16],...arr[17],...arr[18],...arr[19],...arr[20]
};

then it become like original without sorting value

I search and look for many answer but i only saw (how to sort object key) but i don't find any answer about sort object value that work for my javascript object

Sorry, because my language is not English. So, i don't know how to explain in short then my question become very long.

swap=Object.fromEntries(Object.entries(Object.keys(novels).reduce((a, key) => ({...a,[ch[key]]: key}),{})).sort());

Edit: I finally found answer by swap key and value

novels ={
    "Conan's gramology investigator(柯南里的克学调查员)": "39912",
    "Genocide Night, fortunately I have a ninja simulator(灭族之夜,还好我有忍者模拟器)": "10064304",
    "Hokage: Tsunade is pregnant, I can only become stronger!(火影:纲手怀孕了,我只能变强!)": "10061579",
    "King: My ultimate hit rate is 100%(王者:我的大招命中率百分百)": "9982243",
    "Konoha Gatekeeper I was overheard by Tsunade(木叶守门人我被纲手偷听了心声)": "9961541",
    "Life simulator, start with a maid(人生模拟器,开局一宫女)": "46128",
    "Naruto comes to Marvel(降临漫威的火影忍者)": "41052",
    "Naruto: God-level sign-in starts with unlimited chakra(火影:神级签到从无限查克拉开始)": "10064100",
    "Naruto: I can reach full level with one click(火影:我能一键满级)": "10064399",
    "Naruto: My clone can evolve(火影:我的分身能进化)": "10064576",
    "Naruto: Sign in to Reaper's Scythe at the beginning, harvest wildly(火影:开局签到死神之镰,疯狂收割)": "10064894",
    "Naruto: Uchiha, the salted fish, was eavesdropped by Itachi(火影:咸鱼宇智波,被鼬偷听心声)": "10064599",
    "People in Konoha, start life simulator(人在木叶,开局人生模拟器)": "10064579",
    "Pirates: Sign in at the beginning of the Naruto character card(海贼:开局签到火影角色卡)": "10065466",
    "Pokémon: Sign in at the beginning of the Koga Ninja Frog(神奇宝贝:开局签到甲贺忍蛙)": "10064164",
    "Reborn Football Simulator(重生之足球模拟器)": "40859",
    "Simulator: I predict the future in the last days(模拟器:我在末世预测未来)": "40519",
    "The salted fish ninja was overheard by Orochimaru(咸鱼忍者,被大蛇丸偷听心声)": "10064824",
    "Uchiha's Life Simulator(宇智波的人生模拟器)": "43681",
    "Voice of Journey to the West: Beginning Guide Monkey King(西游心声:开局指引孙悟空)": "10072166"
}
rarksoca
  • 31
  • 5
  • you mean can't sort { His comment *(The answer is No)* } but that is 2016 now 2023 may be there is answer – rarksoca Aug 19 '23 at 10:40
  • The answer is __no, you can't__. Native object iterators have their own specific sorting order, properties in an object are not kept in any specific order. The only way you can achieve this is to implement your own object iterator. – Teemu Aug 19 '23 at 10:43
  • The order in which keys are iterated is predefined, and for 32-bit number representations (array indexes), it does not follow insertion order. – trincot Aug 19 '23 at 10:44
  • I suggest you look at using a [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) instead if you want to keep things based on insertion (ie: instead of calling `Object.fromEntries()`, use `new Map()`). – Nick Parsons Aug 19 '23 at 10:45
  • 1
    Relying on object property order is bad practice *in general*. In your case, it's even worse, because your property names are numeric. JavaScript will always present those to you in numeric order, and you cannot change that. – Pointy Aug 19 '23 at 10:45
  • finally found answer from some post that suggest to swap between key and value – rarksoca Aug 19 '23 at 10:53
  • 1
    also see [Does JavaScript guarantee object property order?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – pilchard Aug 19 '23 at 10:54
  • 1
    I think you need to use a Map here. The advantage is that the Map remembers the original insertion order of the keys. This does the job: const sortedNovelsMap = new Map(Object.entries(novels).sort((a, b) => a[1].localeCompare(b[1]))); – academic_failure Aug 19 '23 at 10:59

1 Answers1

0

The iteration order of keys in an object is as follows:

  • Integer keys first, in numerical order
  • Other keys in insertion order

(Integer keys in this context means string representations of positive 32-bit numbers. That's because they could double as array indices.)

Since your keys are integers, you can never sort them in any other way than numerical order in front of other keys.

The solution is to use a different container format for your data, such as an array or a Map (the latter is simply iterated in insertion order and does not have special rules for integer keys). If that's problematic for your use case, you could instead prefix or suffix the keys with some letter, e.g. N39912 or 39912N instead of just 39912 so they are no longer integers.

CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • *"Integer keys first"*: This is not entirely correct. Anyway this topic has been covered many times before on this site. – trincot Aug 19 '23 at 10:46