MathGroup Archive 2000

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

Search the Archive

Re: Sorting

  • To: mathgroup at smc.vnet.net
  • Subject: [mg22088] Re: [mg22067] Sorting
  • From: Andrzej Kozlowski <andrzej at platon.c.u-tokyo.ac.jp>
  • Date: Sun, 13 Feb 2000 01:14:05 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

on 00.2.12 11:53 PM, Andrzej Kozlowski at andrzej at platon.c.u-tokyo.ac.jp
wrote:

> on 00.2.12 6:04 PM, Wagner Truppel at wtruppel at uci.edu wrote:
> 
>> Hi folks,
>> 
>> can anyone tell me how I can make Mathematica sort variable names
>> without sorting any numbers within those variables as strings as
>> well? If you try Sort[{z2,a5,z10}] you get back {a5,z10,z2}, while I
>> want to get back {a5,z2,z10}.
>> 
>> Thanks.
>> Wagner Truppel
>> wtruppel at uci.edu
>> 
> The following is probably not nor most efficient way to do this but it seems
> to work:
> 
> mysort[l_List] :=
> Block[{a, x}, 
> Map[ToExpression,
> Map[StringJoin, 
> Map[ToString, 
> Sort[Map[ToExpression,
> Map[Characters[ToString[#]] &, l], {-1}] /. {a_Symbol,
> x__?NumberQ} -> {a, {x}}], {-1}]]]]
> 
> In[6]:=
> mysort[{a3, z5, z17, a9, b19, b7}]
> 
> Out[6]=
> {a3, a9, b7, b19, z5, z17}
> 
> while
> 
> In[8]:=

> Sort[{a3, z5, z17, a9, b19, b7}]
> 
> Out[8]=
> {a3, a9, b19, b7, z17, z5}
> 
> 
> --


Here is another method, which looks (perhaps) a little more elegant:

In[2]:=
Sort[{z2, a5, z10},
  OrderedQ[{{DeleteCases[ToExpression[Characters[ToString[#1]]], _Integer],
          DeleteCases[
            ToExpression[Characters[ToString[#1]]], _Symbol]}, {DeleteCases[
            ToExpression[Characters[ToString[#2]]], _Integer],
          DeleteCases[ToExpression[Characters[ToString[#2]]], _Symbol]}}] &]

Out[2]=
{a5, z2, z10}

Andrzej Kozlowski
Toyama International University
Toyama, Japan
http://sigma.tuins.ac.jp/




  • Prev by Date: Re: Non-standard evaluation
  • Next by Date: Re: Re: Re: Orientation of characters in FrameLabel
  • Previous by thread: Re: Sorting
  • Next by thread: Re: Sorting