MathGroup Archive 2004

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

Search the Archive

Re: Changing the Natural Sort Order

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49192] Re: Changing the Natural Sort Order
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Wed, 7 Jul 2004 01:42:25 -0400 (EDT)
  • Organization: The University of Western Australia
  • References: <ccb5hr$ekg$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

In article <ccb5hr$ekg$1 at smc.vnet.net>,
 "David Park" <djmp at earthlink.net> 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]]}

Cheers,
Paul

-- 
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 physics.uwa.edu.au 
AUSTRALIA                            http://physics.uwa.edu.au/~paul


  • Prev by Date: Re: Solid plot
  • Next by Date: Re: XML Importing
  • Previous by thread: RE: Changing the Natural Sort Order
  • Next by thread: Re: Re: Changing the Natural Sort Order