Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #29884

Re: Bubblesort

Path csiph.com!weretis.net!feeder4.news.weretis.net!news.albasani.net!.POSTED!not-for-mail
From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Bubblesort
Date Thu, 10 Mar 2016 23:25:07 +0100
Organization albasani.net
Lines 26
Message-ID <nbss83$s16$1@news.albasani.net> (permalink)
References <0d68ee6d-8a80-439e-ae04-17743584e2e4@googlegroups.com> <nbbp99$fed$1@dont-email.me> <XnsA5C17A5C66BA1eejj99@194.109.6.166> <086a7397-459b-46e3-b83c-cfc7edf4188a@googlegroups.com> <7b1ee9db-4732-48e1-be7e-37ba62db4c6e@googlegroups.com> <nbc50g$44d$1@news.albasani.net> <2815605.oGPRxZOVvt@PointedEars.de> <nbnur4$4tr$1@news.albasani.net> <1614595.1D9Jq9LFrv@PointedEars.de> <nbskjb$ehr$1@news.albasani.net> <2712500.u9phrr1jX2@PointedEars.de> <XnsA5C7E51DFC3E5eejj99@194.109.6.166>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding 7bit
X-Trace news.albasani.net lt3+dWtKU+LXKHKSEGHfrN5c+iqNyX7ScA99v8oz88n5FQR1ZUHmoKPqdoDDCiMGTIDIczzjz6Rk60WpNzz5f2XYNhv3yVIgdMIblVKY3amJBQ2QOC29ZNs9uwy8Uryd
NNTP-Posting-Date Thu, 10 Mar 2016 22:25:07 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="EkOuRjRyCvX2euaf45qm0mTeQdvvNqfugRfYE84M2G5DXCwX4lZlUb9+JbDALxiJi4IQjw2XUS73UQSVrjMDq6MSPXHI5D26kTup4+EcvHPglinhPhzL3b1FFMGtB2uL"; mail-complaints-to="abuse@albasani.net"
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1
In-Reply-To <XnsA5C7E51DFC3E5eejj99@194.109.6.166>
Cancel-Lock sha1:bbr9UIeGCjrShbq0OmwncVyPIuk=
X-Enigmail-Draft-Status N1110
Xref csiph.com comp.lang.javascript:29884

Show key headers only | View raw


On 03/10/2016 22:31, Evertjan. wrote:
> This one nicely sorts only the numerical parts in Chrome:
> 
> var arr = [3,2,5,'break',11,8,.5,'break',7,3];
> var res = arr.sort((a, b) => a - b);
> document.write(res); 
> 
> // Chrome: 2,3,5,break,0.5,8,11,break,3,7
> 
> // Edge: 0.5,2,3,5,break,3,8,11,break,7 
> 
> Seems very illogical in Edge, what is happening?

This comparison function is unsuitable for non-numeric elements. If you
want to sort mixed-type arrays, you need to to define how your elements
compare to each other. Is "break" larger or smaller than 8? Is an object
larger or smaller than `true`?

The expression `a - b` evaluates to NaN if one or both of the operands
are non-numeric strings. As of ES2015, this is specified to be
interpreted as +0 by Array.prototype.sort. In other words, "break" in
your example is seen as equal to every other array element in the array.
That's not a stable sorting algorithm, and the result is what you see in
your test.

- stefan

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Bubblesort jonas.thornvall@gmail.com - 2016-03-04 02:00 -0800
  Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 18:53 +0800
    Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 12:01 +0100
      Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 03:03 -0800
      Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 05:15 -0800
        Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 05:41 -0800
          Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 21:56 +0800
          Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-04 15:10 +0100
            Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 07:37 -0800
              Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-04 17:08 +0100
            Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 07:50 -0800
            Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 07:54 -0800
              Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-04 17:21 +0100
              Re: Bubblesort Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-04 19:26 +0000
                Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 23:05 +0100
                Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-05 09:15 -0800
                Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-08 03:43 +0100
                Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-08 02:41 -0800
                Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-08 09:10 -0800
                Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-08 23:34 +0100
                Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-08 23:46 +0100
                Re: Bubblesort Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-09 00:16 +0000
                Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-09 02:41 +0100
            Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-08 23:35 +0100
              Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-09 02:38 +0100
                Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-09 22:04 +0100
                Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-10 21:14 +0100
                Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-10 22:17 +0100
                Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-10 22:31 +0100
                Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-10 23:25 +0100
                Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-11 00:16 +0100
        Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 21:59 +0800
          Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 17:12 +0100
          Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-05 02:00 +0100
            Re: Bubblesort John Harris <niam@jghnorth.org.uk.invalid> - 2016-03-05 18:39 +0000
            Re: Bubblesort Scott Sauyet <scott.sauyet@gmail.com> - 2016-03-05 17:40 -0800
      Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 21:54 +0800
        Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 16:59 +0100
    Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 03:04 -0800
      Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 03:55 -0800
    Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-05 00:01 +0100
      Re: Bubblesort Aleksandro <aleksandro@gmx.com> - 2016-03-06 18:41 -0300

csiph-web