Mathematica 9 is now available
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: Changing the Natural Sort Order

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49167] Re: [mg49144] Changing the Natural Sort Order
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Tue, 6 Jul 2004 03:33:28 -0400 (EDT)
  • References: <200407050854.EAA14743@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 5 Jul 2004, at 17:54, David Park wrote:

> *This message was transferred with a trial version of CommuniGate(tm) 
> Pro*
> 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/
>
It seems to me that  your desired ordering is not defined well enough. 
It sounds like you want to change the ordering of just selected 
symbols, but you do not make clear how the other symbols should then be 
treated. You give this example:

> Assuming[d < b < c, Sort[{a, b, c, d, f}]]
>
> giving the desired output
>
> {a,d,b,c,f}

which suggests that the remaining symbols should remain unchanged. This 
is clear enough when all the elements you are reordering are 
contiguous. If you mean to consider only such cases than your problem 
is quite easy to solve. But if not, then what should happen in this 
case:

Assuming[c < a < d, Sort[{a, b, c, d, f}]] ?

It is not clear whether you keep the relation a<b, in which case b must 
now be larger than c, or the relation b<c , which would mean that now 
a>b. In other words, do you want your output to be
{ b,c,a,d, f}
  or
{c,a,b,d,f}
?

What you seem to want is an ordering that changes the order of some 
symbols and preserves the natural ordering of the rest, but you also 
have to say how the elements that are specified to be reordered compare 
with the ones that have not been included in the specification.There 
are of course lots of ways of dealing with this problem, but something 
more needs to be said. For example, one approach that could be used is 
to treat all symbols specified in Assuming as being earlier (or later) 
than the other symbols. That certainly will give a well defined 
ordering, so that

Assuming[d < b < c, Sort[{a, b, c, d, f}]]

would now be

{d,b,c,a,f} and

Assuming[c < a < d, Sort[{a, b, c, d, f}]]

would be

{c,a,d,b,f}

and so on.

Once you agree to do this it is easy to sort the symbols (for example, 
you could replace them by integers in the same order, then use Sort, 
then replace the integers back by the original symbols). In fact you 
could use this method for sorting the expressions too. In any case,
once you know how to sort the symbols you can sort the expressions in 
various ways, for example like this:


<<DiscreteMath`Combinatorica`

MySort[expr_,ls_List]:=Sort[expr][[InversePermutation[Ordering[ls]]]]

This works as follows:


MySort[{h[x, g[a]], h[x, g[b]], h[x, g[c]], h[x, g[d]],
    h[x, g[f]]}, {a, d, b, c, f}]


{h[x, g[a]], h[x, g[d]], h[x, g[b]], h[x, g[c]],
   h[x, g[f]]}

And so on.

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


  • Prev by Date: Re: How Many Users Are Happy With FindRoot?
  • Next by Date: Re: How Many Users Are Happy With FindRoot?
  • Previous by thread: Changing the Natural Sort Order
  • Next by thread: Re: Changing the Natural Sort Order