Message Boards Message Boards

0
|
1952 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

About the results returned by SortBy and ReverseSortBy

Posted 1 year ago

See my following example:

In[1437]:= sol
ReverseSortBy[sol, Abs@*Last]
ReverseSortBy[sol, -Abs@*Last]
SortBy[Abs@*Last]@sol 
SortBy[-Abs@*Last]@sol 

Out[1437]= {{x -> 1}, {x -> 5/4}, {x -> 5/8}}

Out[1438]= {{x -> 5/4}, {x -> 1}, {x -> 5/8}}

Out[1439]= {{x -> 5/4}, {x -> 1}, {x -> 5/8}}

Out[1440]= {{x -> 5/8}, {x -> 1}, {x -> 5/4}}

Out[1441]= {{x -> 5/8}, {x -> 1}, {x -> 5/4}}

Why doesn't the negative order with "-" take effect at all?

Regards,
Zhao

POSTED BY: Hongyi Zhao
2 Replies
Posted 1 year ago

Thank you for pointing this out. It seems that all the following forms are equivalent:

In[692]:= {x->1}//Values//First
{x->1}//Last//Last
{x->1}//First//Last

Out[692]= 1

Out[693]= 1

Out[694]= 1

Therefore, I further collate the possible solutions to the problem here:

In[695]:= sol = {{x -> 1}, {x -> 5/4}, {x -> 5/8}};
ReverseSortBy[sol, Minus@*Abs@*Last@*First]
ReverseSortBy[sol, Minus@*Abs@*First@*Values]

SortBy[sol, Minus@*Abs@*Last@*First]
SortBy[sol, Minus@*Abs@*Last@*Last]

Out[696]= {{x -> 5/8}, {x -> 1}, {x -> 5/4}}

Out[697]= {{x -> 5/8}, {x -> 1}, {x -> 5/4}}

Out[698]= {{x -> 5/4}, {x -> 1}, {x -> 5/8}}

Out[699]= {{x -> 5/4}, {x -> 1}, {x -> 5/8}}

In[731]:= 
s1={{x -> 1}, {x -> -5/4}, {x -> 5/8}};
ReverseSortBy[s1, (#//Values//First//Abs)&] 
SortBy[s1, (#//Values//First//Abs)&] 

Out[732]= {{x -> -(5/4)}, {x -> 1}, {x -> 5/8}}

Out[733]= {{x -> 5/8}, {x -> 1}, {x -> -(5/4)}}

Regards, Zhao

POSTED BY: Hongyi Zhao
Posted 1 year ago

{x->1}//First and {x->1}//Last returns the same thing. Namely the whole rule itself.

Also the negation, "-" in your code, should be a part of the function composition in order to work.

In[1]:= sol = {{x -> 1}, {x -> 5/4}, {x -> 5/8}};
ReverseSortBy[sol, Minus@*Abs@*Last@*First]
SortBy[sol, Minus@*Abs@*Last@*First]

Out[2]= {{x -> 5/8}, {x -> 1}, {x -> 5/4}}
Out[3]= {{x -> 5/4}, {x -> 1}, {x -> 5/8}}
POSTED BY: Hans Milton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract