Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Changing the Natural Sort Order

  • To: mathgroup at
  • Subject: [mg49184] Re: Changing the Natural Sort Order
  • From: ab_def at (Maxim)
  • Date: Tue, 6 Jul 2004 03:34:28 -0400 (EDT)
  • References: <ccb5hr$ekg$>
  • Sender: owner-wri-mathgroup at

"David Park" <djmp at> wrote in message news:<ccb5hr$ekg$1 at>...
> Dear MathGroup,
> Is it possible to change the natural sort order of symbols that is used in Sort?
> I would like something like the following statement (that does not work).
> Assuming[d < b < c, Sort[{a, b, c, d, f}]]
> giving the desired output
> {a,d,b,c,f}
> I won't be sorting simple lists of symbols, but lists of similar, but unspecified, expressions that contain the symbols. For example...
> {h[x,g[a]], h[x,g[b]], h[x,g[c]], h[x,g[d]], h[x,g[f]]}
> which should give
> {h[x,g[a]], h[x,g[d]], h[x,g[b]], h[x,g[c]], h[x,g[f]]}
> Is there any way to do this?
> David Park
> djmp at

The first thing to try when dealing with assumptions is to use Refine:

Assuming[b < a && b > c,
  Sort[{a, b, c}, Refine[# < #2]& ]]

{c, b, a}

In general, this won't work if the test is undefined for some pairs as
in your example:

Assuming[a > b,
  Sort[{a, x, b}, Refine[# < #2]& ]]

{a, x, b}

If you have h[x,g[a]] instead of a, then you can simply use
#[[2,1]]<#2[[2,1]] instead of #<#2, or you can pull out the occurences
of a,b,c with Cases.

However, it is hard to guarantee anything with Refine; even a
simplification of the form Refine[Sign[expr],expr>0] may fail:

Refine[Sign[x1*y1+x2*y2+x3*y3], x1*y1+x2*y2+x3*y3 > 0]


So you're probably better off just defining a function which will
return the desired ordering of the elements, without relying on

You can use Reduce for more complicated cases:

Assuming[1 < E^x < E,
  Sort[{x, 0, 1},
    Reduce[ForAll[x, $Assumptions, # < #2], Reals]& ]]

{0, x, 1}

Maxim Rytin
m.r at

  • Prev by Date: Re: RE: Re: Bug in FromDate
  • Next by Date: RE: Changing the Natural Sort Order
  • Previous by thread: Re: Changing the Natural Sort Order
  • Next by thread: RE: Changing the Natural Sort Order