Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #7960
| Message-ID | <8931625.SEqChMirdb@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2011-11-03 20:41 +0100 |
| Subject | Re: sorting columns |
| Newsgroups | comp.lang.javascript |
| References | <f7fca7c8-769a-4339-ae49-bda395338ef3@l12g2000vby.googlegroups.com> <mqUGzLEbPZsOFwUD@invalid.uk.co.demon.merlyn.invalid> <j8uonk$v6j$1@news.albasani.net> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
Andreas Bergmaier wrote:
> Dr J R Stockton schrieb:
>> Why sort a<table>?
> 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<tr.length; 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<length; i++)
> table.insertRow(r[i][1]);
>
> Disclaimer: I'm not sure about standard-compliancy of all browsers
> regarding * <table>.rows
> * <table>.insertRow(<tr>)
> * <tr>.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] <http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-html.html>
<http://msdn.microsoft.com/en-us/library/ms537484(VS.85).aspx>
[2] <http://pointedears.de/scripts/test/es-matrix/#a>
[3] <https://developer.mozilla.org/En/DOM/Node.textContent>
<http://msdn.microsoft.com/en-us/library/ff974773(VS.85).aspx>
--
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
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
sorting columns fulio pen <fuliopen@yahoo.com> - 2011-11-01 06:14 -0700
Re: sorting columns Elegie <elegie@anonymous.invalid> - 2011-11-01 20:18 +0100
Re: sorting columns fulio pen <fuliopen@yahoo.com> - 2011-11-02 05:07 -0700
Re: sorting columns Elegie <elegie@anonymous.invalid> - 2011-11-02 13:46 +0100
Re: sorting columns fulio pen <fuliopen@yahoo.com> - 2011-11-02 07:39 -0700
Re: sorting columns Elegie <elegie@anonymous.invalid> - 2011-11-02 16:00 +0100
Re: sorting columns fulio pen <fuliopen@yahoo.com> - 2011-11-02 11:06 -0700
Re: sorting columns Dr J R Stockton <reply1144@merlyn.demon.co.uk> - 2011-11-02 19:02 +0000
Re: sorting columns Elegie <elegie@anonymous.invalid> - 2011-11-03 02:27 +0100
Re: sorting columns Andreas Bergmaier <andber93@web.de> - 2011-11-03 20:06 +0100
Re: sorting columns Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-03 20:41 +0100
Re: sorting columns Dr J R Stockton <reply1144@merlyn.demon.co.uk> - 2011-11-04 21:12 +0000
Re: sorting columns Dr J R Stockton <reply1144@merlyn.demon.co.uk> - 2011-11-04 21:00 +0000
Re: sorting columns Dr J R Stockton <reply1144@merlyn.demon.co.uk> - 2011-11-03 21:03 +0000
csiph-web