The way JavaScript sorts tables or elements by text, numbers, or dates.

Source: Internet
Author: User
Tags join sort sorts table name

This example describes how JavaScript sorts tables or elements by text, numbers, or dates. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231-232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262-2 63 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298-299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329-3 30 331 332 333 334 335 336 337 338 339 340 341 342 343 344 Sorting table columns correctly by text, number or date. There are other//versions, plugins, etc., for this but they the either are restricted to specific//date formats, or Requir E EVERY row-element to be given a sort attribute; Mine//can handle many different date and number formats, and even allows for specific//cells ' may not ' conform to T He overall date/number the format for that column. My version also enables sorting of element Hierarchies:such as a DIV containing//p-paragraphs and Spans-this could Even is an image-gallery containing prices//or dates within spans. Very Efficient as well!! example:andrew.dx.am/sortgallerydel.html//Addsorttotables (); 'll make the table headers clickable, to sort columns in//ascending or descending order, for any tables with class= "Sor Tit ". SortTable (TBL, col); Would sort the table (TBL is a ID or Table object) by//The supplied column index (indexed from 0) in ascending or Descen Ding order. Addsortbydate (TBL, col, DAtemask); Enables sorting of a column by date,///specified by a date-mask such as ' Dd-mmm-yy '. Addsortbynumber (TBL, col); Enables sorting of a column by number. This assumes A//period. As the decimal separator (if present); It ignores any other non-numeric//characters. Sortelements (Parentel, Childtag, Coltag, Colindex); would sort (non-table)//elements in ascending or descending order. For example, an UL containing LIs//and spans. COLINDEX specifies which span to sort; There may is more than one in//the LI (0 indexed). Example:sortelements (' divID ', ' p ', ' span ', 2); 3rd span within each paragraph. AddSortByDate2 (Parentel, Childtag, Coltag, Colindex, Datemask); and//AddSortByNumber2 (Parentel, Childtag, Coltag, Colindex)//provide the same feature-set as Addsortbydate and AddSort Bynumber does//for tables, but for element hierarchies. If There are dates or numbers in a column (or element) which don ' t meet the//Date-mask or number formatting necessary To sort correctly, then these individual//elements can is given the attribute "sort" and they would still sort correctly ! For example, with a date column <td sort= "2012/12/20" > would still sort A//cell correctly. This format ' YYYY/MM/DD ' is converted into a Date () object.) var monthnames = ["January", "February", "March", "April", "may", "June", "July", "August", "September", "October", "Novem ber "," December "]; var daynames = ["Sunday", "Monday", "Tueday", "Wednesday", "Thursday", "Friday", "Saturday"]; var shortmths = ["No More", "Feb", "Mar", "APR", "may", "June", "O", "Aug", "Sep", "Oct", "Nov", "Dec"]; var shortdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; var addevent = function (Elem, EventType, func) {//Helper function. if (elem.addeventlistener) addevent = function (el EM, EventType, func) {Elem.addeventlistener (EventType, Func, false); else if (elem.attachevent) addevent = function (Elem, EventType, func) {elem.attachevent (' on ' + EventtypE, func); }; else addevent = function (Elem, EventType, func) {elem[' on ' + eventtype] = func;}; Addevent (Elem, EventType, func); }; Sort methods/algorithms Attributed:var sorttable = function (tbl, col) {//could be called directly (sortelements, ' TR ', ' TD ', Col; }; var sortelements = function (Parentel, Childtag, Coltag, Colindex) {//Example use:sortelements (' table1 ', ' tr ', ' TD ', 2)// or sortelements (' List1 ', ' li ')//or sortelements (' divname ', ' p ', ' span ', 3) var i, J, cTags = {}, startAt = 0, Childlen, aCh ILD, Elem, sortby, content, Elems = [], Sortedlen, Frag, Hdrslen, HDR; var parent = (typeof Parentel = = ' string ')? document.getElementById (Parentel): Parentel; var asctext = function (A, B) {//Sort () by. data as text var x = a.data, y = b.data, Xnum = parsefloat (x), ynum = Parsef Loat (y); Check if each begin with a number ... if (!isnan (xnum) &&!isnan (ynum) && (xnum-ynum)) return xnum-ynum; Return ((x < y)?-1: ((x > Y)? 1:0)); }; VarDesctext = function (A, B) {//Sort () by. data var x = a.data, y = b.data, Xnum = parsefloat (x), ynum = parsefloat (y); Check if each begin with a number ... if (!isnan (xnum) &&!isnan (ynum) && (ynum-xnum)) return ynum-xnum; Return ((x > Y)?-1: ((x < y)? 1:0)); }; var ascnum = function (A, B) {//used with dates as OK return a.data-b.data;}; var descnum = function (A, b) {return b.data-a.data;}; if (parent.nodeName.toLowerCase () = = ' table ') {if (Childtag = = ' tr ') {sortby = Parent.rows[0].cells[colindex].sortby | | ' Text '; Parent = Parent.tbodies[0] | | Parent if (parent.rows[0].cells[0].nodename.tolowercase () = = ' th ') {startAt = 1;}} CTags = Parent.getelementsbytagname (Childtag); if (typeof Colindex = = ' undefined ') {sortby = ' text ';//Sort simple lists or paragraphs as text} for (i = startAt, ch Ildlen = Ctags.length; i < Childlen; i++) {///.. Go forward examining each child achild = ctags[i]; Elem = (coltag)? Achild.geteleMentsbytagname (Coltag) [Colindex]: achild; if (elem) {if (!sortby) {//sorting non-table columns. SortBy = (typeof elem.numbervalue '!= ')? ' Number ': ((typeof elem.datevalue!= ' undefined ')? ' Date ': ' Text '); Switch (sortby) {//Can supply ' sort ' attributes to enable sorting of numbers, etc./For example, <td sort= ' 20 11/02/12 ' > for a date. Case ' text ': content = (Elem.getattribute (' sort ') | | elem.firstChild.nodeValue). toLowerCase (); Break Case ' number ': content = Elem.numbervalue; Break Case ' Date ': content = Elem.datevalue; Break Default:content = (Elem.getattribute (' sort ') | | | elem.firstChild.nodeValue). toLowerCase (); Break } j = Elems.length; if (!achild.id) achild.id = ' Tempsortid ' + j; Elems[j] = {data:content, tempID:aChild.id}; }//The following'll determine if the TABLE/ETC has already been//by the sorted column or tag. If So, it would sort in ascending or descending//order. It creates custom element properties to the Parent element to//Remember the previous sort details. if (typeof Colindex = = ' undefined ') colindex = 0; if (parent.prevtag && Parent.prevtag = = (typeof Coltag = = ' undefined ') (Childtag:coltag)) {if (PARENT.PREVC OL = = Colindex) {//sorting by the same column as previously parent.prevsort = (Parent.prevsort = ' asc ')? ' desc ': ' ASC '; else {//sorting by any other column Parent.prevcol = colindex; parent.prevsort = ' ASC ';}} else {//sorting for the "1st time" or by a different tag Parent.prevtag = (typeof Coltag = ' undefined ')? childtag:col TAG); Parent.prevcol = Colindex; Parent.prevsort = ' ASC '; } if (Parent.prevsort = = ' desc ') {//' desc ' is the previous sort order ... switch (sortby) {case ' text ': elems.sor T (Desctext); Break Case ' number ': Elems.sort (Descnum); Break Case ' Date ': Elems.sort (Descnum); Break Default:elems.sort (Desctext); Break } else {switch (sortby) {case ' text ': Elems.sort (Asctext), break, case ' number ': Elems.sort (ascnum); BreaK Case ' Date ': Elems.sort (Ascnum); Break Default:elems.sort (Asctext); Break } Frag = Document.createdocumentfragment (); for (i = 0, Sortedlen = elems.length i < Sortedlen; i++) {elem = document.getElementById (elems[i].tempid); Frag.appen Dchild (Elem); if ((elem.id). substr (0,10) = = ' Tempsortid ') elem.removeattribute (' id '); } parent.appendchild (Frag); Elems = null; return parent.prevsort; not currently used}; var addsorttotables = function () {//.. If table has Class-name ' sortit ' var tables = document.getelementsbytagname (' table '), I, J, Tbllen, TBL, HDRs, Hdrslen; function Preservesortscope (a,b,c,d) {return function () {//Assign-sortelements fn. to a table header sortelements (a , B, C, D); }//Add sorting to table headers for (i = 0, Tbllen = tables.length i < Tbllen; i++) {tbl = Tables[i]; if (tbl. Classname.indexof (' sortit ') + 1) {HDRs = tbl.getelementsbytagname (' th '); if (HDRs) {for (j = 0, Hdrslen = hdrs.length ; J < Hdrslen; J + +) {addevent(Hdrs[j], ' click ', Preservesortscope (TBL, ' tr ', ' TD ', j)); If there ' s no title already, add "click to sort" if (!hdrs[j].title) hdrs[j].setattribute (' title ', ' Click to sort '); } } } } }; var addsortbydate = function (tbl, col, datemask) {//Input:the table name (or object), a column index (or array)//and A date mask (' dd-mmm-yy ')//Adds a sortby = ' Date ' property to the ' the ' the ' the ' the ' the ' the ' the ' the ' the ' the ' I s a header row var i, Rlen, cell; while (col.length) addsortbydate (Tbl,col.pop (), datemask); if ((col instanceof Array) | | | isNaN (COL)) return; var tbl = (typeof tbl = = ' string ')? document.getElementById (TBL): TBL; Tbl.rows[0].cells[col].sortby = ' Date '; AddSortByDate2 (TBL, ' tr ', ' TD ', Col, Datemask); }; var AddSortByDate2 = function (Parentel, Childtag, Coltag, Colindex, datemask) {var kids, startAt = 0, I, Rlen, cell; var Parent = (typeof Parentel = = ' string ')? document.getElementById (Parentel): Parentel; if (parent.nodeName.toLowerCase () = = ' table ') { Parent = Parent.tbodies[0] | | Parent StartAt = (parent.rows[0].cells[0].nodename.tolowercase () = = ' th ') * 1; } kids = Parent.getelementsbytagname (Childtag); for (i = startAt, Rlen = Kids.length i < Rlen; i++) {cell = Kids[i].getelementsbytagname (Coltag) [Colindex]; if (cell {if (typeof cell.numbervalue!= ' undefined ') delete cell.numbervalue;//The above enables A date sort//(although v. unlikely) if (Cell.getattribute (' sort ')) {//Use the sort attribute if present Cell.datevalue = New Date (Cell.getattribute (' sort ')); else {cell.datevalue = new Date (Stringtodate (Cell.firstChild.nodeValue, Datemask));} if (cell.dateValue.toString () = "NaN" | | cell.dateValue.toString () = = "Invalid Date") {cell.datevalue = 0;}} } }; var addsortbynumber = function (tbl, col) {//COL is a column index or array of indices/would ignore the Uming It is a header row var i, Rlen, cell, tempnum; while (col.length) Addsortbynumber (Tbl,col.pop ()); IF ((col instanceof Array) | | | isNaN (COL)) return; TBL = (typeof tbl = = ' string ')? document.getElementById (TBL): TBL; Tbl.rows[0].cells[col].sortby = ' number '; AddSortByNumber2 (TBL, ' tr ', ' TD ', COL); }; var AddSortByNumber2 = function (Parentel, Childtag, Coltag, Colindex) {var kids, startAt = 0, I, Rlen, cell, tempnum; VA R parent = (typeof Parentel = = ' string ')? document.getElementById (Parentel): Parentel; if (parent.nodeName.toLowerCase () = = ' table ') {parent = Parent.tbodies[0] | | parent; startAt = (parent.rows[0].cells[0) . nodename.tolowercase () = = ' th ') * 1; } kids = Parent.getelementsbytagname (Childtag); for (i = startAt, Rlen = Kids.length i < Rlen; i++) {cell = Kids[i].getelementsbytagname (Coltag) [Colindex]; if (cell) {if (typeof cell.datevalue!= ' undefined ') delete cell.datevalue;//The above enables the switching from a date to a numb ER sort//(although v. unlikely) Tempnum = Cell.getattribute (' sort ') | | Cell.firstChild.nodeValue; Tempnum = Tempnum.replace (/[^0-9.-)/g, ""); Cell.numbervalue = parsefloat (tempnum); if (isNaN (cell.numbervalue)) Cell.numbervalue = 0.0; } } }; var stringtodate = function (Sdate, Sformat, cutOff) {//Input:a date value as a string, it ' s format as a string e.g. ' d D-mmm-yy '//optional:a cutoff (integer) for 2 digit years. If no ' d ' appears in the format string then the 1st of the month is assumed. If the cut-off is then the value would be converted//to 2020; If the then this would be converted to 1940. If no cut-off is supplied then ' would be pre-pended to the year (YY). Output:a string in the format ' yyyy/mm/dd ' or '/won't be attempt to convert certain combinations e.g. DMM, MDD, DD M, Yyyyd. var sparsed, Fndsingle; Sparsed would be constructed in the format ' yyyy/mm/dd ' sdate = sdate.tostring (). toUpperCase (); Sformat = Sformat.touppercase (); if (Sformat.search (/mmmm| mmm/) + 1) {//replace mar/march with, etc. sdate = sdate.replace (new RegExp (' + shortmths.Join (' | ') + ') [a-z]* ', ' gi '], function (m) {var i = shortmths.indexof (M.charat (0). toUpperCase () + M.SUBSTR (1, 2). Tolowerc ASE ()) + 1; Return ((I < 10)? "0" + I: "" + i). toString (); Sformat = Sformat.replace (/mmmm| mmm/g, ' MM '); } if (Sformat.search (/dddd| ddd/) + 1) {//replace Tue/tuesday, etc. with ' Sdate = Sdate.replace (new RegExp (' + shortdays.join (' | ') + ') [a-z]* ', ' GI '), '); Sformat = Sformat.replace (/dddd| Ddd/g, ""); } sdate = Sdate.replace (/(^| d) (d) (=d|$)/g, function ($, $, $) {//single digits 2 with 0 ' + $}); Sformat = Sformat.replace (/(^|[ ^dmy]) (d| M) (=[^dmy]|$)/g, function ($, $) {return $ + $ + $//replace D or M with DD and MM}); Are there still single Ds or Ms? Fndsingle = Sformat.search (/(^|[ ^D]) D ([^d]|$) | (^| [^m]) M ([^m]|$)/) +1; if (Fndsingle) return "; Don't attempt to parse, for example, ' DMM ' Sformat = Sformat.replace (/(^|[ ^y]) (YY) (? =[^y]|$)/g, function ($, $, $, index) {var tempdate = sdate.substr (0, Index +1); Tempdate + = (cutOff)? ((parseint (SDATE.SUBSTR (index + 1, 2), ten) > CutOff)? ' 19 ': ' 20 '): ' 20 '; Tempdate + = SDATE.SUBSTR (index + 1); Sdate = tempdate; Return $ + $ + $; }); sparsed = (' Yyyy/mm/dd '). Replace (/yyyy| mm| dd/g, function (m) {return (Sformat.indexof (m) + 1)? Sdate.substr (Sformat.indexof (M), m.length): ";}"; if (Sparsed.charat (0) = = '/') {//If no specified, assume the current year sparsed = (new Date (). getFullYear ()) + SP arsed; } if (Sparsed.charat (sparsed.length-1) = = '/') {//If no date, assume the 1st of month sparsed + + ';}//should End up with characters ... return (Sparsed.length = 10)? sparsed: '; };

The

wants this article to help you with your JavaScript programming.

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.