Re: Changing the Natural Sort Order
- To: mathgroup at smc.vnet.net
- Subject: [mg49184] Re: Changing the Natural Sort Order
- From: ab_def at prontomail.com (Maxim)
- Date: Tue, 6 Jul 2004 03:34:28 -0400 (EDT)
- References: <ccb5hr$ekg$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"David Park" <djmp at earthlink.net> wrote in message news:<ccb5hr$ekg$1 at smc.vnet.net>... > 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/ The first thing to try when dealing with assumptions is to use Refine: In[1]:= Assuming[b < a && b > c, Sort[{a, b, c}, Refine[# < #2]& ]] Out[1]= {c, b, a} In general, this won't work if the test is undefined for some pairs as in your example: In[2]:= Assuming[a > b, Sort[{a, x, b}, Refine[# < #2]& ]] Out[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: In[3]:= Refine[Sign[x1*y1+x2*y2+x3*y3], x1*y1+x2*y2+x3*y3 > 0] Out[3]= Sign[x1*y1+x2*y2+x3*y3] So you're probably better off just defining a function which will return the desired ordering of the elements, without relying on assumptions. You can use Reduce for more complicated cases: In[4]:= Assuming[1 < E^x < E, Sort[{x, 0, 1}, Reduce[ForAll[x, $Assumptions, # < #2], Reals]& ]] Out[4]= {0, x, 1} Maxim Rytin m.r at inbox.ru