localeCompare und negative Zahlen – SELFHTML-Forum Forum als Ergänzung zum SELFHTML-Wiki und zur Dokumentation SELFHTML https://forum.selfhtml.org/self localeCompare und negative Zahlen Mon, 18 Feb 19 07:03:50 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742846#m1742846 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742846#m1742846 <p>Hallo,</p> <p>auf <a href="http://test.j-berkemeier.de/selfwiki/Sortierbare%20Tabelle/Beispiel_JS-Anw-sortierbare-Tabellen_lc-num.html" rel="nofollow noopener noreferrer">dieser Seite</a> verwende ich zum Vergleichen die Methode <a href="https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare" rel="nofollow noopener noreferrer">localeCompare</a> in der Form <code>return a[sortiert].localeCompare(b[sortiert],doclang,{numeric:true});</code>, also mit dem Parameter <code>numeric:true</code>.</p> <p>Leider wird die Spalte mit den negativen Zahlen falsch sortiert. Verwende ich die Funktion falsch, oder kennt localeCompare keine negativen Zahlen?</p> <p>Gruß<br> Jürgen</p> <p>PS doclang hat den Wert "de";</p> localeCompare und negative Zahlen Mon, 18 Feb 19 12:22:32 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742881#m1742881 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742881#m1742881 <blockquote> <p>Verwende ich die Funktion falsch, oder kennt localeCompare keine negativen Zahlen?</p> </blockquote> <p>String.localeCompare() ist eine Stringfunktion. MFG</p> localeCompare und negative Zahlen Mon, 18 Feb 19 12:31:34 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742883#m1742883 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742883#m1742883 <p>Hallo JürgenB,</p> <blockquote> <p>Leider wird die Spalte mit den negativen Zahlen falsch sortiert. Verwende ich die Funktion falsch, oder kennt localeCompare keine negativen Zahlen?</p> </blockquote> <p>Kann ich nicht nachvollziehen. Bei mir geht das:</p> <pre><code class="block language-js"><span class="token punctuation">[</span><span class="token string">"1"</span><span class="token punctuation">,</span> <span class="token string">"3"</span><span class="token punctuation">,</span> <span class="token string">"-1"</span><span class="token punctuation">,</span> <span class="token string">"0"</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token operator">=></span> a<span class="token punctuation">.</span><span class="token function">localeCompare</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token string">"de"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">numeric</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// ergibt: (4) ["-1", "0", "1", "3"]</span> </code></pre> <p><strong>Edit:</strong> ah, verstehe - du meinst innerhalb der negativen Zahlen. Ja, das ist bei mir auch falsch. Nein, es sieht nicht so aus als machst du etwas falsch. Ich würde das als Bug oder zumindest als irreführende Parametrisierung einstufen: <code>{numeric: true}</code> legt nahe, dass ein Vergleich von Nummer stattfindet und nicht mehr von Strings. Ist aber offensichtlich nicht der Fall. Mal sehen, was der Standard dazu sagt…</p> <p>LG,<br> CK</p> <div class="signature">-- <br> <a href="https://wwwtech.de/about" rel="noopener noreferrer">https://wwwtech.de/about</a> </div> localeCompare und negative Zahlen Mon, 18 Feb 19 12:43:37 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742885#m1742885 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742885#m1742885 <p>Hallo,</p> <blockquote> <p>String.localeCompare() ist eine Stringfunktion.</p> </blockquote> <p>Stimmt. -1.234 aus dem DOM gelesen ist ein String. Meine Hoffnung war, das es als Zahl interpretiert wird.</p> <p>Gruß<br> Jürgen</p> localeCompare und negative Zahlen Mon, 18 Feb 19 12:41:39 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742884#m1742884 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742884#m1742884 <p>Hallo Christian,</p> <p>danke. Ich werde dann wohl localeCompare nur für Nicht-Zahlen nehmen, und für Zahlen die alte Funktion behalten müssen. Schade.</p> <p>Gruß<br> Jürgen</p> localeCompare und negative Zahlen Mon, 18 Feb 19 12:48:48 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742886#m1742886 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742886#m1742886 <p>Hallo JürgenB,</p> <blockquote> <p>danke. Ich werde dann wohl localeCompare nur für Nicht-Zahlen nehmen, und für Zahlen die alte Funktion behalten müssen. Schade.</p> </blockquote> <p>Ich hab jetzt mal im Standard geblättert… auf den ersten Blick sieht das nach „unterspezifiziert“ aus. Der sagt zum eigentlichen Vergleich nämlich gar nichts aus. Der sagt nur das hier:</p> <blockquote> <p>The two Strings are compared in an implementation-defined fashion.</p> </blockquote> <p>Und:</p> <blockquote> <p>The result is intended to order String values in the sort order specified by the effective locale and collation options computed during construction of collator, and will be negative, zero, or positive, depending on whether x comes before y in the sort order, the Strings are equal under the sort order, or x comes after y in the sort order, respectively.</p> </blockquote> <p>Und:</p> <blockquote> <p>[[Numeric]] is a Boolean value, specifying whether numeric sorting is used.</p> </blockquote> <p>Kein Wort darüber, wie eigentlich genau verglichen werden soll. Ich könnte mir sogar vorstellen, dass das abhängig von OS und Umgebung/Locale ist…</p> <p>LG,<br> CK</p> <div class="signature">-- <br> <a href="https://wwwtech.de/about" rel="noopener noreferrer">https://wwwtech.de/about</a> </div> localeCompare und negative Zahlen Mon, 18 Feb 19 13:06:07 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742887#m1742887 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742887#m1742887 <p>Hallo Christian,</p> <p>noch mal danke fürs nachsehen.</p> <blockquote> <blockquote> <p>[[Numeric]] is a Boolean value, specifying whether numeric sorting is used.</p> </blockquote> <p>Kein Wort darüber, wie eigentlich genau verglichen werden soll. Ich könnte mir sogar vorstellen, dass das abhängig von OS und Umgebung/Locale ist…</p> </blockquote> <p>Aber -3 sollte in allen Sprachen und Systemen kleiner als -2 sein.</p> <p>Wäre schön gewesen, im Tutorial auf die Unterscheidung zwischen Text und Zahl verzichten zu können.</p> <p>Gruß<br> Jürgen</p> localeCompare und negative Zahlen Mon, 18 Feb 19 13:23:40 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742889#m1742889 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742889#m1742889 <p>Hallo JürgenB,</p> <blockquote> <p>Aber -3 sollte in allen Sprachen und Systemen kleiner als -2 sein.</p> </blockquote> <p>Ja. Wie gesagt: ich halte das für unterspezifiziert.</p> <p>Ich vermute, dass das <code>-</code> nicht als Teil einer Zahl erkannt wird und deshalb ein Fallback auf einen „normalen“ Vergleich gemacht wird. Da der Standard dazu aber nichts sagt, kann man auch nicht sagen, dass das falsch ist…</p> <p>LG,<br> CK</p> <div class="signature">-- <br> <a href="https://wwwtech.de/about" rel="noopener noreferrer">https://wwwtech.de/about</a> </div> localeCompare und negative Zahlen Mon, 18 Feb 19 13:33:03 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742890#m1742890 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742890#m1742890 <p>Hallo,</p> <blockquote> <p>Ich vermute, dass das <code>-</code> nicht als Teil einer Zahl erkannt wird und deshalb ein Fallback auf einen „normalen“ Vergleich gemacht wird. Da der Standard dazu aber nichts sagt, kann man auch nicht sagen, dass das falsch ist…</p> </blockquote> <p>Würde dann nicht auf Jürgens Beispielseite die -1 zusammen mit -12 und -15 sortiert sein?</p> <p>Gruß<br> Kalk</p> localeCompare und negative Zahlen Mon, 18 Feb 19 13:51:14 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742892#m1742892 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742892#m1742892 <p>Hallo Tabellenkalk,</p> <blockquote> <blockquote> <p>Ich vermute, dass das <code>-</code> nicht als Teil einer Zahl erkannt wird und deshalb ein Fallback auf einen „normalen“ Vergleich gemacht wird. Da der Standard dazu aber nichts sagt, kann man auch nicht sagen, dass das falsch ist…</p> </blockquote> <p>Würde dann nicht auf Jürgens Beispielseite die -1 zusammen mit -12 und -15 sortiert</p> </blockquote> <p>Ja, das ist doch auch der Fall:</p> <p><a href="/images/269ed958-8a3e-48ad-9060-0f3c794b9826.png" rel="noopener noreferrer"><img src="/images/269ed958-8a3e-48ad-9060-0f3c794b9826.png?size=medium" alt="" loading="lazy"></a></p> <p>LG,<br> CK</p> <div class="signature">-- <br> <a href="https://wwwtech.de/about" rel="noopener noreferrer">https://wwwtech.de/about</a> </div> localeCompare und negative Zahlen Mon, 18 Feb 19 13:55:22 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742893#m1742893 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742893#m1742893 <p>Hallo,</p> <blockquote> <p>Würde dann nicht auf Jürgens Beispielseite die -1 zusammen mit -12 und -15 sortiert sein?</p> </blockquote> <p>das sieht für mich so aus, als würden im Text Zahlen erkannt, -12 ist ein Minus mit einer Zwölf dahinter. Leider wurden negative Zahlen wohl vergessen bzw. nicht spezifiziert.</p> <p>Gruß<br> Jürgen</p> localeCompare und negative Zahlen Mon, 18 Feb 19 13:55:44 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742894#m1742894 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742894#m1742894 <p>Hallo,</p> <blockquote> <p>Ja, das ist doch auch der Fall:</p> </blockquote> <p>Es zeigt, dass als Zahl sortiert wird, und nicht zu Strings gefallbackt wird. An sonsten würde -1, -12, -15, -2… sortiert werden.</p> <p>Gruß<br> Kalk</p> localeCompare und negative Zahlen Mon, 18 Feb 19 14:01:40 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742895#m1742895 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742895#m1742895 <p>Hallo Tabellenkalk,</p> <blockquote> <blockquote> <p>Ja, das ist doch auch der Fall:</p> </blockquote> <p>Es zeigt, dass als Zahl sortiert wird, und nicht zu Strings gefallbackt wird. An sonsten würde -1, -12, -15, -2… sortiert werden.</p> </blockquote> <p>Nein, wenn als Zahl sortiert würde, dann würde -15, -12, … sortiert werden. Es wird nach Text zurück gefallen.</p> <p>Und da <code>-15</code>, <code>-12</code>, … alle ein - davor haben, sind die vor den reinen Zahlen. Innerhalb dieser Gruppe wird dann nach dem darauf folgenden Zeichen sortiert – und das ist wieder eine Zahl. Sorry, ich dachte, es wäre offensichtlich, was ich meinte.</p> <p>LG,<br> CK</p> <div class="signature">-- <br> <a href="https://wwwtech.de/about" rel="noopener noreferrer">https://wwwtech.de/about</a> </div> localeCompare und negative Zahlen Mon, 18 Feb 19 14:14:21 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742896#m1742896 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742896#m1742896 <p>Hallo,</p> <blockquote> <p>ich dachte, es wäre offensichtlich, was ich meinte.</p> </blockquote> <p>Weil ich mit einem so ... abwechslungsreichen Algorithmus nicht gerechnet habe</p> <p>Gruß<br> Kalk</p> localeCompare und negative Zahlen Mon, 18 Feb 19 14:18:56 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742897#m1742897 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742897#m1742897 <p>Hallo Tabellenkalk,</p> <blockquote> <blockquote> <p>ich dachte, es wäre offensichtlich, was ich meinte.</p> </blockquote> <p>Weil ich mit einem so ... abwechslungsreichen Algorithmus nicht gerechnet habe</p> </blockquote> <p>Naja, es entbehrt nicht einer gewissen Logik.</p> <p>Aber wie gesagt – sorry. Nächstes mal bin ich ausführlicher. </p> <p>LG,<br> CK</p> <div class="signature">-- <br> <a href="https://wwwtech.de/about" rel="noopener noreferrer">https://wwwtech.de/about</a> </div> localeCompare und negative Zahlen Mon, 18 Feb 19 14:34:58 Z https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742898#m1742898 https://forum.selfhtml.org/self/2019/feb/18/localecompare-und-negative-zahlen/1742898#m1742898 <p>Hallo,</p> <blockquote> <p>... abwechslungsreichen Algorithmus ...</p> </blockquote> <p> </p> <p>Gruß<br> Jürgen</p>