Thanks a lot for the clarification, Lasse and Mads.

Jungshik

On Tue, Mar 15, 2011 at 1:03 AM, Lasse R.H. Nielsen <[email protected]>wrote:

> There is no way to directly pass coll.compare without losing its attachment
> to coll.
>
> Your own example below shows what to do instead:
>    words.sort(function(a,b) { return coll.compare(a,b); })
> This ensures that the compare function is called as a member of coll, not
> as a plain function.
>
> You can also (but I don't really recommend it) use the ES5
> Function.prototype.bind function:
>   words.sort(coll.compare.bind(coll))
> It basically does the same as the above function, just with more overhead
> because the bind function does
> more than just bind the receiver.
>
> Best of luck
> /L
>
> On Tue, Mar 15, 2011 at 00:23, <[email protected]> wrote:
>
>> Thank you for the explanation.
>>
>> Is there any way to make 'array.sort(coll.compare)' work on my side (in
>> C++) or
>> is that the way sort() is supposed to work no matter what? It seems that
>> the
>> latter is the case, but I'd better ask you to make sure.
>>
>> I guess many callers want to define their own compare function any way
>> because
>> they're likely to compare more complex objects than simple strings. For
>> instance, a file browser may want to list directories before regular files
>> and
>> would use the following:
>>
>> var compareFileName = function(a, b) {
>>   if (a.type == b.type) { return coll.compare(a.name, b.name);}
>>   if (a.type =="directory") return -1;
>>   return 1;
>> }
>>
>> So, not being able to directly pass coll.compare() is probably ok.
>> However, if
>> there's any way to make it possible, pls let me know.
>>
>>
>>
>> http://codereview.chromium.org/6673011/
>>
>> --
>> v8-dev mailing list
>> [email protected]
>> http://groups.google.com/group/v8-dev
>>
>
>
>
> --
> Lasse R.H. Nielsen
> [email protected]
> 'Faith without judgement merely degrades the spirit divine'
> Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K -
> Denmark - CVR nr. 28 86 69 84
>

-- 
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to