Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'How to sort Strings in SubLists?' topicStudent Support Forum > General > Archives > "How to sort Strings in SubLists?"

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic
Author Comment/Response
Nasser M. Abbasi
01/26/12 01:12am

Sorting by the first Element (String): undefinied ???
In[21]:= Sort[{{"aa", 2}, {"cc", 1}, {"bb", 3}}, #1[[1]] < #2[[1]] &]
Out[21]= {{"aa", 2}, {"cc", 1}, {"bb", 3}}

How can i get the answer:
{{"aa", 2}, {"bb", 3}, {"cc", 1}}
==========================

Use OrderQ:

OrderedQ[{"cc", "bb"}]
==> False

OrderedQ[{"bb", "cc"}]
==>True

hence, your sort will become as follows

Sort[{{"aa", 2}, {"cc", 1}, {"bb", 3}},
OrderedQ[{First@#1, First@#2}] &]

===> {{"aa", 2}, {"bb", 3}, {"cc", 1}}

---Nasser


URL: ,

Subject (listing for 'How to sort Strings in SubLists?')
Author Date Posted
How to sort Strings in SubLists? centurion 01/25/12 09:28am
Re: How to sort Strings in SubLists? Nasser M. Ab... 01/26/12 01:12am
Re: How to sort Strings in SubLists? Bill Simpson 01/26/12 01:18am
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic