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: [mg49179] RE: [mg49144] Changing the Natural Sort Order
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Tue, 6 Jul 2004 03:34:00 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: David Park [mailto:djmp at earthlink.net]
To: mathgroup at smc.vnet.net
>Sent: Monday, July 05, 2004 10:54 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg49179] [mg49144] Changing the Natural Sort Order
>
>
>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/ 
>
>
>
>

David,

this is not ready (haven't got the time right now), but to pass the idea:

In[114]:= ClearAll[Assuming]
In[115]:=
Attributes[Assuming] = HoldAll;

Assuming[Less[seq__], Sort[expr_]] :=
  Module[{restss},
    With[{newss = {seq},
        oldss = Sort[{seq}],
        allss = Union[Cases[expr, _Symbol, Infinity]]},
      restss = Complement[allss, oldss];
      Block[allss, newss = Hold /@ oldss; Evaluate[restss] = Hold /@ restss;
            Sort[expr]] /. Thread[Hold /@ oldss -> newss] /. 
        Thread[Hold /@ restss -> restss]]]

In[117]:= Assuming[d < b < c, Sort[{a, b, c, d, f}]]
Out[117]= {a, d, b, c, f}

In[118]:= 
Assuming[d < b < c, 
  Sort[{h[x, g[a]], h[x, g[b]], h[x, g[c]], h[x, g[d]], h[x, g[f]]}]]
Out[118]=
{h[x, g[a]], h[x, g[d]], h[x, g[b]], h[x, g[c]], h[x, g[f]]}


Resting problems: this only works on symbols at leaves (not at Heads), and doesn't work for symbols that cannot be localized, e.g. List.  Also, there occur problems when Hold already wraps a symbol. 


--
Hartmut


  • Prev by Date: Re: Changing the Natural Sort Order
  • Next by Date: Re: Normal distribtion
  • Previous by thread: Re: Changing the Natural Sort Order
  • Next by thread: Re: Changing the Natural Sort Order