Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*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 2005

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

Search the Archive

Re: String comparison

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55034] Re: [mg55009] String comparison
  • From: "Christoph Lhotka" <lhotka at astro.univie.ac.at>
  • Date: Thu, 10 Mar 2005 05:24:38 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

The solution to your problem ist the function OrderedQ:

In[..]:=Sort[alist, OrderedQ[{#1[[3]], #2[[3]]}] &]
Out[..]:={{c, 2, abc}, {a, 3, bca}, {b, 1, cba}}

OrderedQ[{a,b}] returns true, if a and b are in lexikograohic order, and false
otherwise, you can use it for strings and expressions:

In[..]:=OrderedQ[{b,a}]
Out[..]:=False

In[..]:=OrderedQ[{"a","b"}]
Out[..]:=True

with kind regards ...

On Wed, 9 Mar 2005 06:34:34 -0500 (EST)
 yasdfer at yahoo.com wrote:
> Hi,
> 
> I want to sort a list/table based on an arbitrary "column".
> 
> In[1]:= alist = {{a, 3, "bca"}, {b, 1, "cba"}, {c, 2, "abc"}};
> 
> If I want to sort based on say the second column, I can use:
> 
> In[2]:= Sort[alist, #1[[2]] > #2[[2]] &]
> Out[2]:= {{a, 3, bca}, {c, 2, abc}, {b, 1, cba}}
> 
> With the string column however, the method fails:
> 
> In[3]:= Sort[alist, #1[[3]] > #2[[3]] &]
> Out[3]:= {{a, 3, bca}, {b, 1, cba}, {c, 2, abc}}
> 
> I can get away with,
> 
> In[4]:= Reverse[RotateRight[#, 2] & /@ Sort[RotateLeft[#, 2] & /@
> alist]]
> Out[4]:= {{b, 1, cba}, {a, 3, bca}, {c, 2, abc}}
> 
> Question is, what function can I pass to Sort[] directly to avoid the
> bending and twisting?
> 
> In general, how does one compare strings for lexical ordering? Noticed
> that, Greater["a", "b"] does not evaluate either.
> 
> Thanks.
> 


-- Christoph Lhotka --
University of Vienna
Institute for Astronomy
Tuerkenschanzstr. 17 
1180 Vienna, Austria
fon. +43.1.4277.518.41
mail. lhotka at astro.univie.ac.at


  • Prev by Date: Re: Canonical order...
  • Next by Date: Re: Nested iterators in Compile
  • Previous by thread: Re: String comparison
  • Next by thread: Re: String comparison