Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <9570287.vLAVuxmI2c@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sun, 05 May 2013 01:00:16 +0200 User-Agent: KNode/4.8.5 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <12786665.ZbpLptdNGN@PointedEars.de> <4320221.o7mTL0IePv@PointedEars.de> <42a7e3a8-0fc0-4a5d-8809-312503f14f28@z10g2000yqd.googlegroups.com> MIME-Version: 1.0 Lines: 175 NNTP-Posting-Date: 05 May 2013 01:00:17 CEST NNTP-Posting-Host: da785330.newsspool3.arcor-online.net X-Trace: DXC=m[n\eQdG[gLI7\_^6>c20JMcF=Q^Z^V3H4Fo<]lROoRA8kF Thomas 'PointedEars' Lahn wrote: >> Scott Sauyet wrote: >>> Thomas 'PointedEars' Lahn wrote: >>>> ______ >>>> ¹ However, this gives me the idea that it could be useful that >>>> jsx.array.getComparator() could also be passed a callback or >>>> constructor for each key, or all keys, whose return value would be used >>>> as operand in the comparison. What do you think? >>> >>> I'm not sure what you would mean by "or all keys" unless you mean for >>> each independently, >> >> Currently you can configure the sort method (numeric or generic), sort >> order (ascending or descending), and strictness (strict or loose >> equality) of the sorting both for each key separately and for all keys >> specified in the factory call. The same approach would be applied to the >> callback/constructor options. > > Yes, I did understand the code. I just didn't see how supplying the > same constructor or callback for all properties would be useful. > Which means, I think, that I'm probably misunderstanding in general. [This method, which is a (Function) factory, is now jsx.array.createComparator() to avoid confusion with getters.] If the values of all (sort) keys are of the same type, and they are to be compared as if they were of another type, it should not be necessary to write var comparator = jsx.array.createComparator([ {key: "x", callback: myConverter}, {key: "y", callback: myConverter} {key: "z", callback: myConverter} ]); instead of var comparator = jsx.array.createComparator(["x", "y", "z"], { callback: myConverter }); Same for “constructor” and “comparator”. DRY. >>> If the objects you want to sort have a property more complex than a >>> String or Number but that still has an intrinsic order, it would be nice >>> to be able to include that in your comparator. But rather than a >>> callback/constructor, how about recursively applying another comparator >>> for that key? >> >> I do not see how this could work. The elements of the Array are to be >> sorted by a property of the objects referred by the Array elements; the >> properties of the object's that are referred by the Array's elements are >> not to be modified. > > Perhaps I'm misunderstanding your suggestion. ISTM you understand correctly. > My idea is probably best given with an example. If you had an array of > objects structured something like this: > > myCar { > year: 2003, > manufacturer: "Dodge", > make: "Neon", > color: "red", > engine: { > liters: 2.0, > cylinders: 4 > }, > doors: 4 > // ... > } > > Then I might want to make a comparator with code something like this > (please forgive me if I misuse your API; I haven't actually tried it): > > var myComparator = jsx.array.getComparator([ > "year", > "manufacturer", > "model", > { > key: "engine", > comparator: jsx.array.getComparator([ > "cylinders", > "liters" > ]) > } > ]); > > That internal `getComparator` call was what I was suggesting. It > seems to me to be a great way of offereing additional flexibility. Yes, that is a Good Idea. It is easier to implement and use, and more efficient than having the outer comparator iterate over subkeys. Implemented. > Is this similar to what you wanted to do? Would it cover the same > ground? It's clearly not as powerful as offering an arbitrary > function, but it might do much of what one would want to with such a > function, no? [It turns out that one can eat this cake *and* have it.] If the “callback” property is specified, the original sort value is converted by passing the value to it, and using its return value as the new sort value. If the “constructor” (own) property is specified, the original or previous sort value is converted by passing it to the constructor, and using the returned object value as the new sort value. If the “comparator” property is specified, it takes precedence over the default comparison, using the original sort value or one of the previous sort values. If either the outer key is the last key, or if the inner comparator returns a value not equal to 0, the return value of the specified comparator is returned; otherwise the values of the next keys are considered. That is, function Bar (baz) { this.baz = String(baz); } Bar.prototype.toString = function () { return this.baz; }; function Baz (bla) { this.length = bla.length; } var foo = [ {"bar": new Bar("burpbarbazbla")}, {"bar": new Bar("foo")} ]; var _createComparator = jsx.array.createComparator; foo.sort(_createComparator(["bar"], { callback: String, constructor: Baz, comparator: _createComparator(["length"], {numeric: true}) })); results in “foo” holding a reference to the equivalent of [ {"bar": new Bar("foo")}, {"bar": new Bar("burpbarbazbla")} ] because: 1a. String(new Bar("burpbarbazbla")) === "burpbarbazbla" 1b. String(new Bar("foo")) === "foo" 2a. new Baz("burpbarbazbla") returns {length: 13} : Baz 2b. new Baz("foo") returns {length: 3} : Baz 3a. new Baz("burpbarbazbla").length === 13 3b. new Baz("foo").length === 3 4. 3 < 13 The unit test is now available at . -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.