JavaScript objects are sorted by key value

Source: Internet
Author: User

Recently encountered a very interesting problem in the work. The object for JS is sorted by key value. such as the following Obj object

var obj ={
"China": "' Beijing ', ' Sichuan ', ' Jilin '",
"British Great Britain (2)": "' London ', ' Airland '",
"California, USA (5)": "' Phonix ', ' atalata '",
"Deutsche (4)": "' Berlin ', ' etc '"
}

Initially encountering this problem, I was trying to get all the key values through a traversal, push to an array, and then sort the arr.sort () on the arrays.

But I can not directly to ' China 100 ', ' Deutsche 4 ', sort because ASCII code or what should not support the Chinese language, (what is the reason, and then delve into it later).

So I need to get the value after the end of Chinese, and then sort the values.

Here I fell into a hole. I took out the Chinese and the number alone, and then I want to sort the numbers and then re-stitch back, and finally through the sequence of key traversal to get the corresponding value, and then the property should be sorted even better ~

Smart you probably already think of what this pit is, and that's not going to help me at all.

If I have more than one key value followed by the same value (e.g. ' Italy 1 ', ' Spain 1 ') What's the point of taking this value out?

Can I still use this value to find the Chinese characters in the front? Or in short, this should be my problem in some aspects ...

Then I thought of rewriting the JS-defined sort () method:

We know that using the sort () method, the simplest format returns an ascending or descending sequence (based on the numeric size):

Sort (A, B) {return a-b/b-a})//a-b to ascending, b-a to descending.

But now I'm wrestling with the question of the Chinese character.

The underlying knowledge may not be solid, or experience is not enough. I didn't think of that. Use substring to remove values and sort the values.

Like this://var reg =/[\u4e00-\u9fa5]*/;

Sort (function (A, b) {

A = a.substring (A.indexof ("("), A.indexof (")"); b= ... return b-a;

})

So the sort () out of the array should be arr=[' Great Britain 2 ', ' Deutsche 4 ', ' California 5 ', ' China 100 ';

And then through the nested for loop,

for (Var i=0;i<arr.length;i++) {

for (i in obj) {

if (arr[i]==i) Arr2.push (' ... ')

}

}

You can iterate over the output of an ordered object property.

(We know that we iterate over the properties in the object, and we usually loop through the properties using for-in, and then we can get each corresponding value.) But the result of for in itself is unordered)--

During this time, I took out the Chinese characters in the key value and looked at some regular expressions. I've had regular contact before, but maybe I don't know what the difference is between text,match,replace.

And I don't know what the values (arrays) are meant to put back into these methods. For example Str.match (REG) [0] so that you can get the text you want, because the match method returns an array,

The first primitive of the array is the text that matches to. (1)

The problem itself is very simple, but why I still took a long time to come out ~ ~ ~:

1. The depth and the way to think about the problem need to be strengthened 2. The level of basic knowledge is not enough 3. Not enough IQ. Not yet diligent.

Finally: Complex problems are made up of simple questions. What we lack is the accumulation of quantity, do not always think of quality change.

JavaScript objects are sorted by key value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.