Max Alekseyev on Tue, 20 Feb 2018 01:39:54 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

vecsort() and sign()


Hello,

In the extended help for 'vecsort' I read:
"a comparison function (t_CLOSURE), with two arguments $x$ and $y$, and returning an integer which is < 0, > 0 or = 0 if x < y, x > y or x=y respectively. The sign function is very useful in this context:"
which is followed by examples like
? vecsort(v, (x,y)->sign(abs(x)-abs(y)))  \\ sort by increasing absolute value
However, I do not see how sign() is useful here. From the definition of vecsort(), it follows that
? vecsort(v, (x,y)->abs(x)-abs(y))
would work equally well. 
So I wonder why enveloping the cmpf function with sign() is "very useful" and why?

Also, the build-in help for vecsort() says:
"If cmpf is an integer, sort according to the value of the k-th component of each entry."
First, it's not clear what is k here. Perhaps, it should say "if cmpf equals an integer k" or something like that.
Second, my first thought was that I can use vecsort() to sort a vector of t_INTMOD based on the value of moduli. Indeed, the modulus of t_INTMOD x equals component(x,1), so supposedly 
? vecsort([Mod(2,3),Mod(1,2)],1)
should work, but it does not.
So, I wonder if vecsort() can be easily extended to sort objects on any types based on the value of component(x,k) (for a given cmpf=k) or it's very specific to vectors and cannot be extended to other types.

Regards,
Max