Javascript table content sorting example code _ javascript tips-js tutorial

Source: Internet
Author: User
This article describes how to sort table content in javascript. For more information, see The Code is as follows:



Script
Var listInfos = new Array ();
ListInfos [0] = new Array ();
ListInfos [0] [0] = {'name': 'recommendation page 1', 'daycount': 666, 'avgtime': 29872, 'errorcount': 180663, 'errorrate': '000000', 'daysystemerrorcount': 0, 'daysystemrerrorrate': '0% '}
ListInfos [0] [1] = {'name': 'recommendation page 2', 'daycount': 593896, 'avgtime': 24946, 'errorcount': 222, 'errorrate': '2% ', 'daysystemerrorcount': 0, 'daysystemrerrorrate': '0% '}
ListInfos [0] [2] = {'name': 'recommendation page 3', 'daycount': 956, 'avgtime': 27957, 'errorcount': 111, 'errorrate': '000000', 'daysystemerrorcount': 0, 'daysystemrerrorrate': '0% '}
ListInfos [1] = new Array ();
ListInfos [1] [0] = {'name': 'recommendation page 4', 'daycount': 666, 'avgtime': 116, 'errorcount': 180663, 'errorrate': '000000', 'daysystemerrorcount': 0, 'daysystemrerrorrate': '0% '}
ListInfos [1] [1] = {'name': 'recommendation page 5', 'daycount': 11, 'avgtime': 222, 'errorcount': 222, 'errorrate': '2% ', 'daysystemerrorcount': 0, 'daysystemrerrorrate': '0% '}
ListInfos [1] [2] = {'name': 'recommendation page 6', 'daycount': 956, 'avgtime': 956, 'errorcount': 111, 'errorrate': '000000', 'daysystemerrorcount': 0, 'daysystemrerrorrate': '0% '}
Function dateDesc (listInfos, field ){
For (var I = 0; I <listInfos. length; I ++ ){
For (var j = I + 1; j <listInfos. length; j ++ ){
If (isCommaPercent (listInfos [I] [field]) <isCommaPercent (listInfos [j] [field]) {
Var arrayTemp = new Array ();
ArrayTemp = listInfos [I];
ListInfos [I] = listInfos [j];
ListInfos [j] = arrayTemp;
}

}
}
Return listInfos;
}
Function dataAsc (listInfos, field ){
For (var I = 0; I <listInfos. length; I ++ ){
For (var j = I + 1; j <listInfos. length; j ++ ){
If (isCommaPercent (listInfos [I] [field])> isCommaPercent (listInfos [j] [field]) {
Var arrayTemp = new Array ();
ArrayTemp = listInfos [I];
ListInfos [I] = listInfos [j];
ListInfos [j] = arrayTemp;
}

}
}
Return listInfos;
}

Function isCommaPercent (value ){
Var valueFloat;
Value = value. toLocaleString ();
ValueFloat = (value. indexOf (',')> 0 )? Value. split (','). join (''): value;
ValueFloat = (valueFloat. indexOf ('%')> 0 )? ParseFloat (valueFloat. substr (0, valueFloat. indexOf ('%'): parseFloat (valueFloat );
Return valueFloat;
}



Function sortOperation (sortInfos, field, sort ){
Var listInfos = new Array ();
If (sort = 'desc '){
For (var I = 0; I <sortInfos. length; I ++ ){
ListInfos [I] = dateDesc (sortInfos [I], field );
}
} Else if (sort = 'asc '){
For (var I = 0; I <sortInfos. length; I ++ ){
ListInfos [I] = dataAsc (sortInfos [I], field );
}
} Else {
Alert ('Operation error ...');
Return false;
}

Var tableStrList = '';
For (var I = 0; I <listInfos. length; I ++ ){
Var tableStr = 'project ';
TableStr + ='













';TableStr + =' ';TableStr + =' ';For (var j = 0; j <listInfos [I]. length; j ++ ){TableStr + =' ';TableStr + =' ';TableStr + =' ';TableStr + =' ';TableStr + =' ';TableStr + =' ';TableStr + =' ';TableStr + =' ';TableStr + =' ';}TableStr + ='
Program name Daily traffic (times) Average response time (us) Number of errors (times)Error Rate (%) Number of system errors (times) System Error Rate (%)
'+ ListInfos [I] [j] ['name'] +''+ ListInfos [I] [j] ['daycount'] +''+ ListInfos [I] [j] ['avgtime'] +''+ ListInfos [I] [j] ['errcount'] +''+ ListInfos [I] [j] ['errorrate'] +''+ ListInfos [I] [j] ['daysystemerrorcount'] +''+ ListInfos [I] [j] ['daysystemrerrorrate'] +'
';
TableStrList + = tableStr
}

Document. getElementById ("html"). innerHTML = tableStrList;
}
SortOperation (listInfos, 'daycount', 'asc ')
Script

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.