Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #29861
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Bubblesort |
| Date | 2016-03-08 23:35 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <2815605.oGPRxZOVvt@PointedEars.de> (permalink) |
| References | (1 earlier) <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> |
Stefan Weiss wrote:
> On 03/04/2016 14:41, jonas.thornvall@gmail.com wrote:
>>> I thought this would sort the property "subarray" nodelinks.
>>>
>>> arr[0].nodelinks.sort();
>>>
>>> But after sort it write out same content as before sort.
>>> 1,10,11,12,13,14,15,16,17,2,3,4,5,6,7,8,9
>>> […]
>
> The result you describe is typically what you get when you use sort()
> with string elements instead of numbers. Ideally, you should make sure
> that you actually insert numbers into `arr[x].nodelinks`, not strings.
As Ben pointed out in <news:8737s6doyh.fsf@bsb.me.uk>, that would not help.
But see also my follow-up to his posting.
> Alternatively, you can sort string array elements using a numeric
> comparison function:
>
> arr[x].nodelinks.sort((a, b) => a - b);
>
> Or in a more verbose form (but with better UA compatibility):
>
> function compareNumeric (a, b)
> {
> return a - b;
> }
>
> arr[x].nodelinks.sort(compareNumeric);
For reasonable backwards-compatibility, the function declaration is
unnecessary/unwise, unless the comparator is declared locally *and* used
locally more than once (in which case a function declaration/variable
initialization saves memory). Function expressions are backwards-compatible
down to JavaScript 1.3 and JScript 5.0 at least (Netscape Navigator 4.06+,
Internet Explorer 5.0), therefore marked “safe” in the ECMAScript Support
Matrix since its inception in 2005 (IIRC).
arr[x].nodelinks.sort(function (a, b) { return a - b; });
<http://PointedEars.de/es-matrix/?filter=function>
This is also the equivalent syntax for the ECMAScript 2015 arrow function
syntax as used above.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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