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


Groups > comp.lang.javascript > #35482

Re: Not understanding "sort" with an argument

Newsgroups comp.lang.javascript
Subject Re: Not understanding "sort" with an argument
From "Evertjan." <exxjxw.hannivoort@inter.nl.net>
References (1 earlier) <d7kqmdpit250iuia6osb956lgva662k3dn@4ax.com> <7fee9579-fe62-4b1d-ac30-61ca33fe32c9@googlegroups.com> <kt96ndddfmkbn0jcv5anhqgb6vlrn4nai5@4ax.com> <XnsA93EE9FA650D8eejj99@194.109.6.166> <dar8nd5gmdugs90v0vhbqsh7n2a4nc3om9@4ax.com>
Date 2018-08-15 21:56 +0200
Message-ID <XnsA93FDF2CC5E2Eeejj99@194.109.6.166> (permalink)

Show all headers | View raw


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)

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


Thread

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

csiph-web