MathGroup Archive 2005

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

Search the Archive

String comparison

  • To: mathgroup at
  • Subject: [mg55009] String comparison
  • From: yasdfer at
  • Date: Wed, 9 Mar 2005 06:34:34 -0500 (EST)
  • Sender: owner-wri-mathgroup at


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] & /@
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.


  • Prev by Date: NIntegrate a COM Object
  • Next by Date: a request for WRI
  • Previous by thread: NIntegrate a COM Object
  • Next by thread: Re: String comparison