Sort by name

Source: Internet
Author: User

July 8, 2014 23:31:59

Today, when I got off work, I was so excited that I didn't have to work overtime. At the moment I finally went home to drink mung bean soup, my sister asked me to hurry up ..

Level-1 departments should be sorted by fixed departments. If not, they should be placed at the end.

In an instant, my passion for going home was ruined. If you have less time to talk about it, go to the topic below.

Specific requirements: Sort level-1 departments in the order of "public media", "public interaction", "Public Entertainment", and "service center", but not at the end of these four departments.

The first thought was to write it in the array, and then find its position cyclically for sorting, but for loop .. I tried to use it as little as possible, and I was confused about the efficiency problem. Finally, I had a chance and suddenly got a wit. I thought of the following method.

For example, I have "three", "five", "one", sorted by "one", "two", "three", "four", "five, the Code is as follows:

1 var A = ["1", "2", "3", "4", "5"]; 2 var B = ["3", "5 ", "1"]; 3 4 var c =. join (); 5 B. sort (function (N1, N2) {6 return c. indexof (N1)-C. indexof (N2); 7}); 8 9 console. log (B); // output: ["1", "3", "5"]

 

So it comes out. However, ifVaR B = ["6", "3", "5", "1"];, The output of B is["6", "1", "3", "5"], Two methods:

Method 1:

1 // change the sort judgment direction, and change the direction of A. 2 3 var A = ["five", "four", "three", "two", "one"]; // direction of a 4 var B = ["six", "three", "five", "one"]; 5 6 var c =. join (); 7 B. sort (function (N1, N2) {8 return C. indexof (N2)-C. indexof (N1); // sort direction 9}); 10 11 console. log (B); // output ["1", "3", "5", "6"]

Method 2:

1 // according to the sort principle: If the return value of the function is smaller than 0, the function changes the position. If the return value is greater than 0, the function does not change the position, stack to the end with 3 4 var A = ["1", "2", "3", "4", "5"]; 5 var B = ["6", "3", "5", "1"]; 6 7 var c =. join (); 8 B. sort (function (N1, N2) {9 var I1 = C. indexof (N1), I2 = C. indexof (N2); 10 if (I1 <0) return 1; // without it, do not change positions, stay behind to go 11 return i1-i2; 12}); 13 14 console. log (B); // output ["1", "3", "5", "6"]

 

Finally, we need to improve it, for example, changing the sorting arrayVaR A = ["1", "2 5", "3", "4 6", "5"];In this case, another value may be retrieved in one value. In this case, you need to add a separator. Here we simply use a comma.

1 // full version (the second method selected, obviously the B-cell is relatively high) 2 3 var A = ["1", "2-5", "3 ", "Four six", "five"]; // in this order, 4 var B = ["six", "three", "five", "one"]; // array 5 var split = ","; // delimiter 6 7 var c = addsplit (. join (split); // ", one, two, five, three, four, six, five," 8 B. sort (function (N1, N2) {9 var I1 = C. indexof (addsplit (N1), 10 I2 = C. indexof (addsplit (N2); 11 if (I1 <0) return 1; 12 Return i1-i2; 13}); 14 15 function addsplit (s) {// both set separators, the boundary \ B in the regular expression is the same as 16 return split + S + split; 17} 18 19 console. Log (B); // output ["1", "3", "5", "6 ~

 

This article is my first technical article. I mainly write my thoughts and feel I am not busy for an hour.

Although not yet, the following sentence should be added, and the link and QR code will be added later:

If you think this article is helpful to you, You can reward me with some money and encourage me to continue writing high-quality blog posts. (QR code) + (Link)

PS: The days have been too leisurely and you have to hurry up...

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.