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


Groups > comp.lang.javascript > #35450 > unrolled thread

Not understanding "sort" with an argument

Started bybit-naughty@hotmail.com
First post2018-08-10 01:11 -0700
Last post2018-08-15 03:58 -0700
Articles 15 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  Not understanding "sort" with an argument bit-naughty@hotmail.com - 2018-08-10 01:11 -0700
    Re: Not understanding "sort" with an argument John G Harris <niam@jghnorth.org.uk.invalid> - 2018-08-10 09:47 +0100
      Re: Not understanding "sort" with an argument bit-naughty@hotmail.com - 2018-08-14 11:25 -0700
        Re: Not understanding "sort" with an argument John G Harris <niam@jghnorth.org.uk.invalid> - 2018-08-14 20:04 +0100
          Re: Not understanding "sort" with an argument "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-08-14 23:00 +0200
            Re: Not understanding "sort" with an argument "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-08-14 23:02 +0200
            Re: Not understanding "sort" with an argument Julio Di Egidio <julio@diegidio.name> - 2018-08-15 04:20 -0700
            Re: Not understanding "sort" with an argument John G Harris <niam@jghnorth.org.uk.invalid> - 2018-08-15 19:15 +0100
              Re: Not understanding "sort" with an argument "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-08-15 21:56 +0200
                Re: Not understanding "sort" with an argument John G Harris <niam@jghnorth.org.uk.invalid> - 2018-08-16 15:24 +0100
                  Re: Not understanding "sort" with an argument Julio Di Egidio <julio@diegidio.name> - 2018-08-16 07:51 -0700
                    Re: Not understanding "sort" with an argument John G Harris <niam@jghnorth.org.uk.invalid> - 2018-08-16 18:51 +0100
                      Re: Not understanding "sort" with an argument Julio Di Egidio <julio@diegidio.name> - 2018-08-16 11:21 -0700
                        Re: Not understanding "sort" with an argument John G Harris <niam@jghnorth.org.uk.invalid> - 2018-08-17 10:44 +0100
    Re: Not understanding "sort" with an argument Julio Di Egidio <julio@diegidio.name> - 2018-08-15 03:58 -0700

#35450 — Not understanding "sort" with an argument

Frombit-naughty@hotmail.com
Date2018-08-10 01:11 -0700
SubjectNot understanding "sort" with an argument
Message-ID<c3146bef-749a-4881-bd24-62e6a9b8df28@googlegroups.com>
..So, my Javascript book has this example code when discussing "custom sorting":

var friends = [
     {name: 'John', age: 30},
     {name: 'Ana', age: 20},
     {name: 'Chris', age: 25}
];

function comparePerson(a,b) {
   if (a.age < b.age) {
      return -1
    }
   if (a.age > b.age) {
      return 1
    } 
   return 0;
}

console.log(friends.sort(comparePerson));





So first of all, John's age is compared to Ana's age, it's more, so "1" is returned - THEN what happens? What are the arguments passed to "sort"? And what does it do?

[toc] | [next] | [standalone]


#35451

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2018-08-10 09:47 +0100
Message-ID<d7kqmdpit250iuia6osb956lgva662k3dn@4ax.com>
In reply to#35450
On Fri, 10 Aug 2018 01:11:56 -0700 (PDT), bit-naughty@hotmail.com
wrote:

>..So, my Javascript book has this example code when discussing "custom sorting":
>
>var friends = [
>     {name: 'John', age: 30},
>     {name: 'Ana', age: 20},
>     {name: 'Chris', age: 25}
>];
>
>function comparePerson(a,b) {
>   if (a.age < b.age) {
>      return -1
>    }
>   if (a.age > b.age) {
>      return 1
>    } 
>   return 0;
>}
>
>console.log(friends.sort(comparePerson));
>
>
>
>
>
>So first of all, John's age is compared to Ana's age, it's more, so "1" is returned - THEN what happens? What are the arguments passed to "sort"? And what does it do?


The function comparePerson has been given to friends.sort, which uses
it to help it do the sorting.

To put it another way : You have told friends.sort which compare
function to use.

Remember that comparePerson without being followed by round brackets
means the function object, not a particular call of the function with
a particular result.

  John

[toc] | [prev] | [next] | [standalone]


#35475

Frombit-naughty@hotmail.com
Date2018-08-14 11:25 -0700
Message-ID<7fee9579-fe62-4b1d-ac30-61ca33fe32c9@googlegroups.com>
In reply to#35451
On Friday, August 10, 2018 at 2:17:08 PM UTC+5:30, John G Harris wrote:
> 
> The function comparePerson has been given to friends.sort, which uses
> it to help it do the sorting.

"help" it *how*?

[toc] | [prev] | [next] | [standalone]


#35476

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2018-08-14 20:04 +0100
Message-ID<kt96ndddfmkbn0jcv5anhqgb6vlrn4nai5@4ax.com>
In reply to#35475
On Tue, 14 Aug 2018 11:25:53 -0700 (PDT), bit-naughty@hotmail.com
wrote:

>On Friday, August 10, 2018 at 2:17:08 PM UTC+5:30, John G Harris wrote:
>> 
>> The function comparePerson has been given to friends.sort, which uses
>> it to help it do the sorting.
>
>"help" it *how*?

Well, when you want to sort things into order you have to be able to
tell whether this thing is before or after that thing. That's what the
comparePerson function does. The sort function does many of these
comparisons in order to do its job of making earlier things come
before later things. 

That's the help the compare function gives to the sort function.

You might like to read up on sort algorithms, but beware, the more
efficient the algorithm the more compicated the code :-(

  John

[toc] | [prev] | [next] | [standalone]


#35477

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2018-08-14 23:00 +0200
Message-ID<XnsA93EE9FA650D8eejj99@194.109.6.166>
In reply to#35476
John G Harris <niam@jghnorth.org.uk.invalid> wrote on 14 Aug 2018 in 
comp.lang.javascript:

> On Tue, 14 Aug 2018 11:25:53 -0700 (PDT), bit-naughty@hotmail.com
> wrote:
> 
>>On Friday, August 10, 2018 at 2:17:08 PM UTC+5:30, John G Harris wrote:
>>> 
>>> The function comparePerson has been given to friends.sort, which uses
>>> it to help it do the sorting.
>>
>>"help" it *how*?
> 
> Well, when you want to sort things into order you have to be able to
> tell whether this thing is before or after that thing. That's what the
> comparePerson function does. The sort function does many of these
> comparisons in order to do its job of making earlier things come
> before later things. 

Not so.

In an array, and this is about sorting an array [or a string posing as an 
array of letters] there is no "earlier" and "later", just difference in 
element position number.
 
> That's the help the compare function gives to the sort function.

No.

The compare function is not just a helper, it defines the algoritm that acts 
as the "worker" of the sort, determining in every internal sorting-step if 
two adjacent elements need to be toggled or not. The sort therefore listens 
to which of the three values the compare returns, *1, 0 or +1 for less than, 
equal end more than.

Such a sort might be alphabetical, numerical, or much more compound.
 
> You might like to read up on sort algorithms, but beware, the more
> efficient the algorithm the more compicated the code :-(

Nonsense, methinks.
What gives you that idea?

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#35478

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2018-08-14 23:02 +0200
Message-ID<XnsA93EEA72C7BDBeejj99@194.109.6.166>
In reply to#35477
"Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote on 14 Aug 2018 in
comp.lang.javascript: 

> to which of the three values the compare returns, *1, 0 or +1 for less
> than, equal end more than.

Sorry:

For *1 read -1 [or in fact any negative numeric value]



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#35480

FromJulio Di Egidio <julio@diegidio.name>
Date2018-08-15 04:20 -0700
Message-ID<c3034925-bb41-4462-ba92-eb3bd537fa60@googlegroups.com>
In reply to#35477
On Tuesday, 14 August 2018 23:00:12 UTC+2, Evertjan.  wrote:
> John G Harris <niam@jghnorth.org.uk.invalid> wrote on 14 Aug 2018 in 
> comp.lang.javascript:
<snip>
> > Well, when you want to sort things into order you have to be able to
> > tell whether this thing is before or after that thing. That's what the
> > comparePerson function does. The sort function does many of these
> > comparisons in order to do its job of making earlier things come
> > before later things. 
> 
> Not so.
> 
> In an array, and this is about sorting an array [or a string posing as an 
> array of letters] there is no "earlier" and "later", just difference in 
> element position number.

It's perfectly fine, and not just in English, to say "earlier" and "later"
"position" in some (here spacial) order.

> > That's the help the compare function gives to the sort function.
> 
> No.
> 
> The compare function is not just a helper, it defines the algoritm that acts 
> as the "worker" of the sort

Nope, the compare function just defines the *order relation*.

> Nonsense, methinks.

Think again.

*Plonk*

Julio

[toc] | [prev] | [next] | [standalone]


#35481

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2018-08-15 19:15 +0100
Message-ID<dar8nd5gmdugs90v0vhbqsh7n2a4nc3om9@4ax.com>
In reply to#35477
On Tue, 14 Aug 2018 23:00:03 +0200, "Evertjan."
<exxjxw.hannivoort@inter.nl.net> wrote:

>John G Harris <niam@jghnorth.org.uk.invalid> wrote on 14 Aug 2018 in 
>comp.lang.javascript:
>
>> On Tue, 14 Aug 2018 11:25:53 -0700 (PDT), bit-naughty@hotmail.com
>> wrote:
>> 
>>>On Friday, August 10, 2018 at 2:17:08 PM UTC+5:30, John G Harris wrote:
>>>> 
>>>> The function comparePerson has been given to friends.sort, which uses
>>>> it to help it do the sorting.
>>>
>>>"help" it *how*?
>> 
>> Well, when you want to sort things into order you have to be able to
>> tell whether this thing is before or after that thing. That's what the
>> comparePerson function does. The sort function does many of these
>> comparisons in order to do its job of making earlier things come
>> before later things. 
>
>Not so.
>
>In an array, and this is about sorting an array [or a string posing as an 
>array of letters] there is no "earlier" and "later", just difference in 
>element position number.

A is before D, D is before Q, Q is before Z.
That's why D,Z,Q,A would be sorted into A,D,Q,Z.


>> That's the help the compare function gives to the sort function.
>
>No.
>
>The compare function is not just a helper, it defines the algoritm that acts 
>as the "worker" of the sort, determining in every internal sorting-step if 
>two adjacent elements need to be toggled or not. The sort therefore listens 
>to which of the three values the compare returns, *1, 0 or +1 for less than, 
>equal end more than.

That's what a 'helper' does. It says less than or greater than or
equal. That's all it does, which is what you are saying in fact. 


>Such a sort might be alphabetical, numerical, or much more compound.

That's why its useful to be able to give the sort function your
preferred comparison function.

 
>> You might like to read up on sort algorithms, but beware, the more
>> efficient the algorithm the more compicated the code :-(
>
>Nonsense, methinks.
>What gives you that idea?

Have you looked at bubble sort and quick sort? If you have you will
find it difficult to convince people that they are equally
complicated. Then there's polyphase merge sort, though that is
unlikely to appear in a browser.

  John

[toc] | [prev] | [next] | [standalone]


#35482

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2018-08-15 21:56 +0200
Message-ID<XnsA93FDF2CC5E2Eeejj99@194.109.6.166>
In reply to#35481
John G Harris <niam@jghnorth.org.uk.invalid> wrote on 15 Aug 2018 in
comp.lang.javascript: 

> On Tue, 14 Aug 2018 23:00:03 +0200, "Evertjan."
> <exxjxw.hannivoort@inter.nl.net> wrote:
> 
>>John G Harris <niam@jghnorth.org.uk.invalid> wrote on 14 Aug 2018 in 
>>comp.lang.javascript:
>>
>>> On Tue, 14 Aug 2018 11:25:53 -0700 (PDT), bit-naughty@hotmail.com
>>> wrote:
>>> 
>>>>On Friday, August 10, 2018 at 2:17:08 PM UTC+5:30, John G Harris
>>>>wrote: 
>>>>> 
>>>>> The function comparePerson has been given to friends.sort, which
>>>>> uses it to help it do the sorting.
>>>>
>>>>"help" it *how*?
>>> 
>>> Well, when you want to sort things into order you have to be able to
>>> tell whether this thing is before or after that thing. That's what the
>>> comparePerson function does. The sort function does many of these
>>> comparisons in order to do its job of making earlier things come
>>> before later things. 
>>
>>Not so.
>>
>>In an array, and this is about sorting an array [or a string posing as
>>an array of letters] there is no "earlier" and "later", just difference
>>in element position number.
> 
> A is before D, D is before Q, Q is before Z.
> That's why D,Z,Q,A would be sorted into A,D,Q,Z.

Not realy, an array has only numbered elements, 
there is no time or spacial location involved.

So "before" is only valid if you add qualifications to it 
that are not there to begin with.

 
>>> That's the help the compare function gives to the sort function.
>>
>>No.
>>
>>The compare function is not just a helper, it defines the algoritm that
>>acts as the "worker" of the sort, determining in every internal
>>sorting-step if two adjacent elements need to be toggled or not. The
>>sort therefore listens to which of the three values the compare returns,
>>*1, 0 or +1 for less than, equal end more than.
> 
> That's what a 'helper' does. It says less than or greater than or
> equal. That's all it does, which is what you are saying in fact. 

Quite.

Such compare function is a necessary part of any sort.

Methinks giving a function the name "helper" does not realy help,
would you cal the wheels of a motor vehicle "helpers" in its locomotion?

>>Such a sort might be alphabetical, numerical, or much more compound.
> 
> That's why its useful to be able to give the sort function your
> preferred comparison function.
> 
>  
>>> You might like to read up on sort algorithms, but beware, the more
>>> efficient the algorithm the more compicated the code :-(
>>
>>Nonsense, methinks.
>>What gives you that idea?
> 
> Have you looked at bubble sort and quick sort?

Yes, I used buble just for my enjoyment to code it mylef liong ago.

> If you have you will
> find it difficult to convince people that they are equally
> complicated.

Looking at it myself has nothing to do with convincing others of anything.

> Then there's polyphase merge sort, though that is
> unlikely to appear in a browser.

However, the innards of the native JS sort() function is not available and 
could even be quite different between JS engines and versions.

An inverse correlation between complexity and efficiency should, imho,
not just be stated, but the arguments of such proposition should be 
explicitly shown.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#35498

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2018-08-16 15:24 +0100
Message-ID<682bnd5tg0smbo34t6jjmmvaa7ua27ulcv@4ax.com>
In reply to#35482
On Wed, 15 Aug 2018 21:56:20 +0200, "Evertjan."
<exxjxw.hannivoort@inter.nl.net> wrote:

  <snip>
>So "before" is only valid if you add qualifications to it 
>that are not there to begin with.

When you want to sort in alphabetical order then A is before B, B is
before C, C is before D, D ...

Does that satisfy your need for extreme nitpicking?


  <snip>
>Methinks giving a function the name "helper" does not realy help,
>would you cal the wheels of a motor vehicle "helpers" in its locomotion?

What about the tyres? There's a choice. Winter tyres help you drive
through snow, summer tyres help you in warmer weather, and sports
tyres help you drive at top speed along German motorways, also Dutch
motorways if they have no speed limit.

Anyway, car analogies are not very approriate for discussing
algorithms.


 <snip>
>An inverse correlation between complexity and efficiency should, imho,
>not just be stated, but the arguments of such proposition should be 
>explicitly shown.

#include <The entire Wikipedia sorting page>

There you are!

  John

[toc] | [prev] | [next] | [standalone]


#35499

FromJulio Di Egidio <julio@diegidio.name>
Date2018-08-16 07:51 -0700
Message-ID<108ad687-a880-406f-9325-8e8c7b5c7128@googlegroups.com>
In reply to#35498
On Thursday, 16 August 2018 16:25:03 UTC+2, John G Harris  wrote:
> On Wed, 15 Aug 2018 21:56:20 +0200, "Evertjan."
> <exxjxw.hannivoort@inter.nl.net> wrote:
<snip>
> >Methinks giving a function the name "helper" does not realy help,
> >would you cal the wheels of a motor vehicle "helpers" in its locomotion?
> 
> What about the tyres? There's a choice. Winter tyres help you drive

"Helper" as you are using it just conveys no meaning: every function is a
helper function by that token, as every function is called by some other
function.  "Helper function" technically is reserved to "little" functions
that just extract a piece of logic from a larger function, usually in order
to make things more readable, or for matters of separations of concerns.  Not
that it is written in stone when a function is a helper function, naming is
still about what a programmer intends to convey: OTOH, there is a language of
the craft that one should learn, at least to start from...

HTH,

Julio

[toc] | [prev] | [next] | [standalone]


#35500

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2018-08-16 18:51 +0100
Message-ID<9debndh9e7dabmijpjuhvsiivo3b2fc9us@4ax.com>
In reply to#35499
On Thu, 16 Aug 2018 07:51:05 -0700 (PDT), Julio Di Egidio
<julio@diegidio.name> wrote:

>On Thursday, 16 August 2018 16:25:03 UTC+2, John G Harris  wrote:
>> On Wed, 15 Aug 2018 21:56:20 +0200, "Evertjan."
>> <exxjxw.hannivoort@inter.nl.net> wrote:
><snip>
>> >Methinks giving a function the name "helper" does not realy help,
>> >would you cal the wheels of a motor vehicle "helpers" in its locomotion?
>> 
>> What about the tyres? There's a choice. Winter tyres help you drive
>
>"Helper" as you are using it just conveys no meaning: every function is a
>helper function by that token, as every function is called by some other
>function.  "Helper function" technically is reserved to "little" functions
>that just extract a piece of logic from a larger function, usually in order
>to make things more readable, or for matters of separations of concerns.  Not
>that it is written in stone when a function is a helper function, naming is
>still about what a programmer intends to convey: OTOH, there is a language of
>the craft that one should learn, at least to start from...


>HTH,

Not really. 

Let's ask the OP if "helps" eventually helped him to understand the
way this sort function operates.

  John

[toc] | [prev] | [next] | [standalone]


#35501

FromJulio Di Egidio <julio@diegidio.name>
Date2018-08-16 11:21 -0700
Message-ID<2e35a959-a4dc-4943-94b0-a09d095a0873@googlegroups.com>
In reply to#35500
On Thursday, 16 August 2018 19:51:44 UTC+2, John G Harris  wrote:
> On Thu, 16 Aug 2018 07:51:05 -0700 (PDT), Julio Di Egidio
> <julio@diegidio.name> wrote:
> >On Thursday, 16 August 2018 16:25:03 UTC+2, John G Harris  wrote:
> >> On Wed, 15 Aug 2018 21:56:20 +0200, "Evertjan."
> >> <exxjxw.hannivoort@inter.nl.net> wrote:
> ><snip>
> >> >Methinks giving a function the name "helper" does not realy help,
> >> >would you cal the wheels of a motor vehicle "helpers" in its locomotion?
> >> 
> >> What about the tyres? There's a choice. Winter tyres help you drive
> >
> >"Helper" as you are using it just conveys no meaning: every function is a
> >helper function by that token, as every function is called by some other
> >function.  "Helper function" technically is reserved to "little" functions
> >that just extract a piece of logic from a larger function, usually in order
> >to make things more readable, or for matters of separations of concerns.  Not
> >that it is written in stone when a function is a helper function, naming is
> >still about what a programmer intends to convey: OTOH, there is a language of
> >the craft that one should learn, at least to start from...
> 
> >HTH,
> 
> Not really. 

Get your head out of your ass first...

*Plonk*

Julio

[toc] | [prev] | [next] | [standalone]


#35509

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2018-08-17 10:44 +0100
Message-ID<356dndltcr7pf1lvu2m9fhgs9qo1j5lcau@4ax.com>
In reply to#35501
On Thu, 16 Aug 2018 11:21:04 -0700 (PDT), Julio Di Egidio
<julio@diegidio.name> wrote:

>On Thursday, 16 August 2018 19:51:44 UTC+2, John G Harris  wrote:
>> On Thu, 16 Aug 2018 07:51:05 -0700 (PDT), Julio Di Egidio
>> <julio@diegidio.name> wrote:
>> >On Thursday, 16 August 2018 16:25:03 UTC+2, John G Harris  wrote:
>> >> On Wed, 15 Aug 2018 21:56:20 +0200, "Evertjan."
>> >> <exxjxw.hannivoort@inter.nl.net> wrote:
>> ><snip>
>> >> >Methinks giving a function the name "helper" does not realy help,
>> >> >would you cal the wheels of a motor vehicle "helpers" in its locomotion?
>> >> 
>> >> What about the tyres? There's a choice. Winter tyres help you drive
>> >
>> >"Helper" as you are using it just conveys no meaning: every function is a
>> >helper function by that token, as every function is called by some other
>> >function.  "Helper function" technically is reserved to "little" functions
>> >that just extract a piece of logic from a larger function, usually in order
>> >to make things more readable, or for matters of separations of concerns.  Not
>> >that it is written in stone when a function is a helper function, naming is
>> >still about what a programmer intends to convey: OTOH, there is a language of
>> >the craft that one should learn, at least to start from...
>> 
>> >HTH,
>> 
>> Not really. 
>
>Get your head out of your ass first...

It's your arse that's got a problem. You say that a helper function is
a little function that just extracts a piece of logic from a larger
function. Well, what the hell do you think the compare function does
inside the sort function ?


>*Plonk*

The usual meaningless noise that does no more than exercise your
fingers.

  John

[toc] | [prev] | [next] | [standalone]


#35479

FromJulio Di Egidio <julio@diegidio.name>
Date2018-08-15 03:58 -0700
Message-ID<ea12b715-0753-4039-8bd0-19e30c7c52c6@googlegroups.com>
In reply to#35450
On Friday, 10 August 2018 10:12:06 UTC+2, bit-n...@hotmail.com  wrote:

> So first of all, John's age is compared to Ana's age, it's more, so "1" is returned - THEN what happens?

Then is no "then", you are simply passing a reference to a function that
compares any two given elements and that's it.  Based on that function,
the sort happens behind the scenes by whichever algorithm with the only
guarantee that the sorting happens in place, not even that it is stable.
If no compare function is specified, the sort is lexicographic over the
elements converted to string.

> What are the arguments passed to "sort"? And what does it do?

To know precisely, always refer to the language specification:
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort>

HTH,

Julio

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web