Call of array. Sort (function () {}) Method

Source: Internet
Author: User

Recently, I am working on a project that mainly involves sorting on pages. This sorting feature involves not only numbers, Chinese Character sorting, but also '-'. This is because the corresponding symbols cannot be obtained.

 

Numeric value, which is displayed using '-' on the page. When sorting, you directly use the Javascript built-in sorting algorithm, that is, Bubble sorting. The basic principle is that you only need to tell me

 

Compare these two values to perform Bubble sorting. Because the number is not large, there is not much time and space complexity. In the middle, I got dizzy because of this.

 

For example, the following is the comparison result:

 

 

  1. Arrayobject. Sort (sortby)

 

Parameters Description
Sortby Optional. Specify the sorting order. Must be a function.

 

 

If no parameter is used when this method is called, the elements in the array are sorted alphabetically. More precise, the elements are sorted by character encoding. To achieve this, first convert the elements of the array into strings (if necessary) for comparison.

If you want to sort by other criteria, you need to provide a comparison function that compares two values and returns a number indicating the relative sequence of the two values. The comparison function should have two parameters, A and B. The returned values are as follows:

 


 

  • If a is smaller than B, A should appear before B in the sorted array,-1 is returned.(A value smaller than 0)
  • If a is equal to B, 0 is returned.
  • If a is greater than B, 1 is returned.(A value greater than 0)

 

 

 

 

Sequence: V1 = 0.41; v2 =-; value =-1;

Sequence: V1 =-; v2 = 0.91; value = 1;

Sequence: V1 =-; v2 =-; value = 0;

Sequence: V1 = 1.26; v2 = 0.56; value = 1
Sequence: V1 = 1.26; v2 = 2.51; value =-1

 

From the preceding sorting result, if it is an order, the preceding value is greater than the subsequent value, and 1 is returned, indicating that the position needs to be changed. If the value following is large,-1 is returned, and no change is required. When you sort '-', perform special processing.

 

Reverse Order: V1 = 1.26; v2 = 5.47; value = 1

Reverse Order: V1 = 1.26; v2 = 0.89; value =-1

Reverse Order: V1 =-; v2 = 2.51; value = 1;

Reverse Order: V1 =-; v2 =-; value = 1;

Reverse Order: V1 = 27.77; v2 =-; value =-1;

 

As shown in the preceding sorting result, if it is in reverse order and the preceding value is greater than the following value,-1 is returned, indicating that the position does not need to be changed. If the value following is large, 1 is returned, and the position is not changed. When you sort '-', perform special processing.



The general method is as follows:
/** <Br/> * sort data <br/> * @ Returns function reference, forward and reverse sorting <br/> */<br/> var domorder = (function () {<br/> return {</P> <p>/** <br/> * for an array of Dom structures, we pass in the index, sort by TD values <br/> * (1) numbers and sort by numbers <br/> * (2) Chinese Characters, sort by Chinese characters <br/> * (3) empty, sort by attribute value <br/> */<br/> comparedomasc: function (INDEX) {<br/> return function (dom1, dom2) {<br/> var value1 =$ (dom1 ). find ("TD" 2.16.eq(index.html .html (); <Br/> var value2 =$ (dom2 ). find ("TD" 2.16.eq(index.html .html (); <br/> // alert ('v = '+ value1 +'; v2 = '+ value2 ); </P> <p> If (value1 = '-' & value2! = '') {<Br/> // writetest ('sequence: V1 = '+ value1 +'; v2 = '+ value2 +'; value = 1 ;'); <br/> return 1; <br/>} else if (value1! = ''& Value2 = '-') {<br/> // writetest ('sequence: V1 = '+ value1 +'; v2 = '+ value2 + '; value =-1; '); <br/> return-1; <br/>} else if (value1 ='-'& value2 = '-') {<br/> // writetest ('sequence: V1 = '+ value1 +'; v2 = '+ value2 +'; value = 0 ;'); <br/> return 0; <br/>} else if (isnan (value1) & isnan (value2) {<br/> return value1.localecompare (value2) *-1; // Chinese Character <br/>} else if (! Isnan (value1 )&&! Isnan (value2) {<br/> // writetest ('sequence: V1 = '+ value1 +'; v2 = '+ value2 +'; value = '+ domorder. comparenum (value1, value2); <br/> return domorder. comparenum (value1, value2); <br/>}else {<br/> var class1 =$ (dom1 ). find ("TD "). eq (index ). ATTR ("class"); // rating <br/> var class2 =$ (dom2 ). find ("TD "). eq (index ). ATTR ("class"); </P> <p> var cv1 = parseint (class1.slice ('-1 ')); <br/> var cv2 = parseint (class2.slice ('- 1 '); <br/> return domorder. comparenum (cv1, cv2); <br/>}</P> <p> // If (value1! = '') {<Br/> // If (isnan (value1) {<br/> // writetest ('sequence: V = '+ value1 + '; v2 = '+ value2 +'; localecompare: value = '+ value1.localecompare (value2) *-1); <br/> // return value1.localecompare (value2) *-1; // Chinese Character <br/> //} else {<br/> // writetest ('sequence: V = '+ value1 + '; v2 = '+ value2 +'; value = '+ domorder. comparenum (value1, value2); <br/> // return domorder. comparenum (value1, value2); <br/>//}< br/>/} </P> <P >}; <br/>}, </P> <p>/** <br/> * for an array of Dom structures, we pass in the index, sort by TD values <br/> * (1) numbers and sort by numbers <br/> * (2) Chinese Characters, sort by Chinese characters <br/> * (3) empty, sort by attribute value <br/> */<br/> comparedomdesc: function (INDEX) {<br/> return function (dom1, dom2) {<br/> var value1 =$ (dom1 ). find ("TD" 2.16.eq(index.html .html (); <br/> var value2 =$ (dom2 ). find ("TD" 2.16.eq(index.html .html (); <br/> // alert ('v = '+ value1 +'; v2 = '+ value2); </P> <p> If (value1 = '-' & value2! = '') {<Br/> // writetest ('reverse order: V1 = '+ value1 +'; v2 = '+ value2 +'; value = 1 ;'); <br/> return 1; <br/>} else if (value1! = ''& Value2 = '-') {<br/> // writetest ('reverse order: V1 = '+ value1 +'; v2 = '+ value2 + '; value =-1; '); <br/> return-1; <br/>} else if (value1 ='-'& value2 = '-') {<br/> // writetest ('reverse order: V1 = '+ value1 +'; v2 = '+ value2 +'; value = 0 ;'); <br/> return 0; <br/>} else if (isnan (value1) & isnan (value2) {<br/> return value1.localecompare (value2) *-1; // Chinese Character <br/>} else if (! Isnan (value1 )&&! Isnan (value2) {<br/> // writetest ('reverse order: V1 = '+ value1 +'; v2 = '+ value2 +'; value = '+ domorder. comparenum (value1, value2) *-1); <br/> return domorder. comparenum (value1, value2) *-1; <br/>} else {<br/> var class1 = $(dom1 ). find ("TD "). eq (index ). ATTR ("class"); // rating <br/> var class2 =$ (dom2 ). find ("TD "). eq (index ). ATTR ("class"); </P> <p> var cv1 = parseint (class1.slice ('-1 ')); <br/> var cv2 = parseint (class2.slice ('-1'); <br/> return domorder. comparenum (cv1, cv2) *-1; <br/>}< br/>}; <br/> }, </P> <p>/** <br/> * compare the two numbers. num1> num2, return 1, = return 0, otherwise, return-1 <br/> */<br/> comparenum: function (N1, N2) {<br/> var num1 = parsefloat (N1 ); <br/> var num2 = parsefloat (N2); </P> <p> If (num1> num2) {<br/> return 1; <br/>} else if (num1 = num2) {<br/> return 0; <br/>} else {<br/> return-1; <br/>}< br/>}; <br/>}) (); <br/> orderedarr = dataarray. sort (domorder. comparedomasc (INDEX ));   To merge the generated files: <? Php <br/> $ dir = 'd: // 3 // '; <br/> if ($ handle = opendir ($ DIR )) {<br/> $ newfile = $ dir. "merge.txt"; </P> <p>/* this is the correct Directory Traversal method */<br/> while (false! ==( $ File = readdir ($ handle) {<br/> // echo "$ File/N"; <br/> $ contents = file_get_contents ($ dir. $ file ). "/N"; <br/> file_put_contents ($ newfile, $ contents, file_append); <br/>}</P> <p> echo $ newfile. "file generated successfully! "; <Br/> closedir ($ handle); <br/>}  At this time, the file will generate a file for sorting.

 

 

 

 

 

 

 

 

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.