Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!news.weisnix.org!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <8931625.SEqChMirdb@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 03 Nov 2011 20:41:25 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: sorting columns Newsgroups: comp.lang.javascript References: Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 76 NNTP-Posting-Date: 03 Nov 2011 20:41:26 CET NNTP-Posting-Host: 13d171f2.newsspool4.arcor-online.net X-Trace: DXC=TbLO1jLA4nTPU8j_I0DN6_4IUK Dr J R Stockton schrieb: >> Why sort a? > Why not? Because that is comparably inefficient. >> Supply the data in JavaScript, as a "two-dimensional array" >> Arr[[..., ..., ... ], [...], [...], [...], ... [...]] ; >> >> At need, sort that. Then remove any existing Table, build the new Table >> using DOM methods, and insert the new Table. > > What do you mean with two-dimensional array? There are no multi-dimensional arrays in ECMAScript implementations, only arrays, represented by Array instances, that can contain references to other Array instances as elements. > Does it represent the cell data, or does each second-level-array contain > the relevant cell content (of the column you want to sort) and a reference > to the row element? Yes. > I guess it would be faster to store the dom elements, instead of > rebuilding them. If you mean references to DOM element objects, that is an interesting idea. The main problem with sorting the table in-situ is that this implicitly requires a considerable number of modifications of the node-relative structure. However, you should take into account the time it takes to access a property of a host object, that is referred to by a property of an Array instance, that yields a primitive, but computed, value, compared to accessing a property of an Array instance that stores a primitive value (the length of the first part of that sentence compared to the length of the second one should make you think already). > A littlebit faster might be something like > r = [], tr = table.rows; > for (var i=1; i r.push( [tr[i].cells[column].textContent, row] ); // slice and map > r.sort(function(a, b) { > return mysortfunction(a[0], b[0]); > }); > for (var i=0; i table.insertRow(r[i][1]); > > Disclaimer: I'm not sure about standard-compliancy of all browsers > regarding *
.rows > *
.insertRow() > * .cells > * <*>.textContent > * Array.prototype.sort(fn) Support for the `rows' and `cells' properties, and Array.prototype.sort() should be ubiquitous by now [1,2]. The `textContent' property, on the other hand, requires an implementation of the Node interface of W3C DOM Level 3 Core, which e. g. MSHTML before version 9 does not provide [3]. PointedEars ___________ [1] [2] [3] -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee