Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: Re: Changing the Natural Sort Order

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49219] Re: [mg49192] Re: Changing the Natural Sort Order
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Thu, 8 Jul 2004 02:50:53 -0400 (EDT)
  • References: <ccb5hr$ekg$1@smc.vnet.net> <200407070542.BAA24966@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 7 Jul 2004, at 14:42, Paul Abbott wrote:

> *This message was transferred with a trial version of CommuniGate(tm)  
> Pro*
> 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
>
>

If this is indeede what David meant (just permuting the selected  
symbols, in this case b,c,d) than it is easy to define an ordering that  
will do this by slightly modifying your approach and using user defined  
ordering function rather than a replacement rule:

orderRule = {d -> b, b -> c, c -> d};

orderedQ[{x_, y_}] := OrderedQ[{x, y} /. orderRule]


Sort[{a,b,c,d,f},orderedQ[{#1,#2}]&]


{a,d,b,c,f}

etc.


Sort[{h[x,g[f]],h[x,g[d]],h[x,g[c]],h[x,g[b]],h[x,g[a]]},orderedQ[{#1,#2 
}]&]


{h(x,g(a)),h(x,g(d)),h(x,g(b)),h(x,g(c)),h(x,g(f))}


etc.


Andrzej Kozlowski
Chiba, Japan
http://www.mimuw.edu.pl/~akoz/


  • Prev by Date: RE: If and openwrite and openappend
  • Next by Date: Re: sorting polynomials by the degree of certain terms
  • Previous by thread: Re: Changing the Natural Sort Order
  • Next by thread: RE: Re: Changing the Natural Sort Order