MathGroup Archive 2004

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

Search the Archive

RE: Re: Changing the Natural Sort Order

Thanks to everybody for the many suggestions on this.

Basically I'm trying to establish a canonical order for a wedge product of
1-forms. The 1-forms may be expressed in several different manners. The
product may contain general symbolic indices and base indices. The base
indices might be {1,2,3) or {0,1,2,3} or {t,x,y,z}. I want the list of base
indices to specify their order. The ones listed are no problem because they
are in natural order. But a user might specify {x,y,z,t} or (in Greek
characters) {rho, theta, phi}, which are not in natural order.

But there are many problems. Using replacement rules can cause difficulties
because the same symbols or values might be in non-indexed positions and
they should not be replaced.

So I have to think about this and will probably have to retreat from trying
to implement such a general capability. Most likely I will have to limit or
specify the possible Mathematica forms of the 1-forms.

David Park
djmp at

From: Paul Abbott [mailto:paul at]
To: mathgroup at

In article <ccb5hr$ekg$1 at>,
 "David Park" <djmp at> wrote:

> 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?

I cannot see a simple way (via OrderedQ) to change the sort order of
symbols. However, here is one way to achieve what you want:

[1] Use Ordering to define a set of replacement rules for your symbols:

  OrderRule[l_] := Thread[l[[Ordering[l]]] -> l]

For your example,

 OrderRule[{d, b, c}]
 {b -> d, c -> b, d -> c}

[2] If you would like to use ordering like d < b < c, you could overload
OrderRule as follows:

  OrderRule[Unevaluated[Less[l__]]] := OrderRule[{l}]

  OrderRule[Unevaluated[Greater[l__]]] := OrderRule[Reverse[{l}]]

[3] Then use Sort followed by OrderRule:

  Sort[{a, b, c, d, f}] /. OrderRule[d < b < c]
  {a, d, b, c, f}

  Sort[{h[x,g[f]], h[x, g[d]], h[x, g[c]], h[x, g[b]], h[x, g[a]]}] /.
    OrderRule[d < b < c]

  {h[x, g[a]], h[x, g[d]], h[x, g[b]], h[x, g[c]], h[x, g[f]]}


Paul Abbott                                   Phone: +61 8 9380 2734
School of Physics, M013                         Fax: +61 8 9380 1014
The University of Western Australia      (CRICOS Provider No 00126G)
35 Stirling Highway
Crawley WA 6009                      mailto:paul at

  • Prev by Date: Re: Normal distribtion
  • Next by Date: Re: Questions about Graph output
  • Previous by thread: Re: Re: Changing the Natural Sort Order
  • Next by thread: partition of 2D arrray