Here's my solution for a customized sort key.  Does this seem reasonable?  
I just copied the format for the sort key for numbers and applied it to 
anything that was real.  It seems to work how I'd want it to work.


def customized_sort_key(item, order=None):
    
    try:
        x=sympify(item)
    except:
        pass
    else:
        if x.is_real:
            return  ((1, 0, 'Number'), (0, ()), (), x)

    return default_sort_key(item,order)




On Saturday, October 18, 2014 8:52:50 PM UTC-5, Duane Nykamp wrote:
>
> I was using sympy's default_sort_order to sort objects.  However, I 
> realized that it does not sort numerical quantities in order:
>
> In [15]: sorted([-1, -1-sqrt(2),1+sqrt(2), pi, 4], key=default_sort_key)
> Out[15]: [-1, 4, pi, 1 + sqrt(2), -sqrt(2) - 1]
>
> The default sort key, lex, works in this case
>
> In [16]: sorted([-1, -1-sqrt(2),1+sqrt(2), pi, 4])
> Out[16]: [-sqrt(2) - 1, -1, 1 + sqrt(2), pi, 4]
>
> but that doesn't work on symbolic objects.
>
> Is there a way to sort that works on all sympy objects, symbolic or not, 
> but still produces the normal order for numerical quantities?
>
> The only thing I can think of is trying the lex sort key first, then 
> reverting to default_sort_key on TypeError.  But, that has the undesirable 
> effect of changing the relative order of numerical quantities once a 
> symbolic quantity is added to the mix.
>
> Any suggestions or pointers to a place where this is discussed already?
>
> Thanks,
> Duane
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/ade686f6-776c-4e6b-a34f-997918afa64c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to