Re: Changing the Natural Sort Order
- To: mathgroup at smc.vnet.net
- Subject: [mg49163] Re: Changing the Natural Sort Order
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Tue, 6 Jul 2004 03:33:21 -0400 (EDT)
- Organization: Universitaet Leipzig
- References: <ccb5hr$ekg$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
- Sender: owner-wri-mathgroup at wolfram.com
Hi, define your order $myOrder = {a, d, b, c, f}; Symbols[expr_] := Union[Cases[expr, _Symbol, Infinity]] orderQ[expr_] := Min[Flatten[Position[$myOrder, #] & /@ Symbols[expr]]] In[]:=Sort[{h[x, g[a]], h[x, g[b]], h[x, g[c]], h[x, g[d]], h[x, g[f]]}, orderQ[#1] < orderQ[#2] &] Out[]={h[x, g[a]], h[x, g[d]], h[x, g[b]], h[x, g[c]], h[x, g[f]]} Regards Jens David Park wrote: > > 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 earthlink.net > http://home.earthlink.net/~djmp/