Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3721040
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:25:13+00:00 2026-05-19T05:25:13+00:00

Please take a look at the following code: Sort[{1, y, x}, Greater] Max[{1, x,

  • 0

Please take a look at the following code:

Sort[{1, y, x}, Greater]
Max[{1, x, y}]
x = 1
y = 2
Sort[{1, y, x}, Greater]
Max[{1, x, y}]

It is interesting to note that the first Sort always produce a definite result while the first Max does not, even when Greater is specified. Note I have not given any numerical values for x and y. Why is this and how can I have a Sort function behave the same way as the Max (or Min) function?

Thanks!

BTW, I am using Mma 7.0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T05:25:15+00:00Added an answer on May 19, 2026 at 5:25 am

    From the help:

    Sort orders symbols by their names, and in the event of a tie, by their context  
    

    So, what you want is another function, not Sort[] …

    Edit

    Here is a way to force only numerical sort under your premises:

    sortN[s_, f_: (OrderedQ[{#1, #2}] &)] :=
    
     (*Check if everything is eval to a number*)
     If[And @@ (NumericQ /@ s),
    
      (*and then sort*)
      Sort[s, f],
    
      (*if something not numeric,
      reconstruct the function call and return it*)
      If[(ToString[f] == ToString[OrderedQ[{#1, #2}] &]), 
        Print[Unevaluated[sortN[s]]], Print[Unevaluated[sortN[s, f]]]];]
    

    Usage:

    In[120]:= sortN[{3,2,1,4}]
    Out[120]= {1,2,3,4}
    
    In[121]:= sortN[{3,2,1,Sin}]
    Out[121]= sortN[{3,2,1,Sin}]
    
    In[122]:= sortN[{1,2,3,4},Greater]
    Out[122]= {4,3,2,1}
    
    In[123]:= sortN[{1,2,3,Log},Greater]
    Out[123]= sortN[{1,2,3,Log},Greater]  
    

    Please be aware that

    1. Each Symbol in the list are going to be evaluated twice, once for checking if it’s numeric, and then for sorting. That could be circumvented by storing the intermediate result before taking the NumericQ. Edit Thinking again, I’m not quite sure … perhaps someone else can clarify on this one.

    2. An expression is considered a numeric quantity if it is either an explicit number or a mathematical constant such as Pi, or is a function that has attribute NumericFunction and all of whose arguments are numeric quantities. In most cases, NumericQ[expr] gives True whenever N[expr] would yield an explicit number.

    3. By changing NumericQ for StringQ or whatever you want (or adding it as a parameter for sortN[], you may select the Type you want to force and sort.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at following code: #include <stdio.h> #include <iostream> using namespace std;
Please take a look at the following code: Public Sub Method(Of TEntity As EntityObject)(ByVal
Please take a look on the following pseudo-code: boolean blocked[2]; int turn; void P(int
Please take a look at the following code: public static void main(String[] args) {
Please take a look at the following code and tell me why skillPercentange is
I have a string with url.Please take a look at the following code.I am
Please take a look at the following .htaccess ErrorDocument 404 /404/ RewriteEngine On RewriteRule
please take a look at the following website: http://www.solidcamxpress.co.uk/ If you look at it
Please take a look at the following JavaScript. I've taken stuff out of it,
Please take a look at this code: class Foo { public $barInstance; public function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.