Re: Sorting a list of pairs on the second elements
- To: mathgroup at smc.vnet.net
- Subject: [mg51345] Re: Sorting a list of pairs on the second elements
- From: "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de>
- Date: Fri, 15 Oct 2004 02:45:37 -0400 (EDT)
- Organization: Uni Leipzig
- References: <cklmi7$f1o$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi, Sort[collectedDnaBin, Last[#1] < Last[#2] &] Regards Jens "János" <janos.lobb at yale.edu> schrieb im Newsbeitrag news:cklmi7$f1o$1 at smc.vnet.net... > Hi, > > I have a list, for example > In[101]:= > collectedDnaBin > > Out[101]= > {{gggcggcgacctcgcgggttttcgctatttatgaaaattttccggtttaaggcgtttccgttcttcttcg > tcataa\ > cttaatgtttttatttaaaatacc,1},{\ > tatttttgtaattcaatactgcttcttcttgcctttgcggaatttggccttttaaaagtatttatcg,13}, > {\ > tatttttgtaattcaatactgcttcttcttgcctttgcggaatttggccttttaaa,12},{\ > tatttttgtaattcaatactgcttcttctt,11},{aaattttccggtttaaggcgtttccgttcttcttcg > tca,\ > 122},{aaattttccggtttaaggcgtttccgttctt,121},{ttcttgcctttgcgg,1211},{\ > actgcttcttcttgcctttgcggaatttg,1221},{\ > gctatttatgaaaattttccggtttaaggcgtttccgttcttcttcgtcataa,133},{ > > gctatttatgaaaattttccggtttaaggcg,132},{gctatttat,131},{gcggaatttggcctttta > aa,\ > 1321},{aaattttcc,13211},{aatactgcttcttcttgcctttgcggaatttggccttttaaaagt,1 > 331},{ > tgaaaattttccggttt,13311}} > > It is a list of pair of Strings. I would like to Sort it on the second > elements of the pairs. > I can do the naive approach with: > In[109]:= > Map[Reverse,Sort[Map[Reverse,collectedDnaBin]]] > > Out[109]= > {{gggcggcgacctcgcgggttttcgctatttatgaaaattttccggtttaaggcgtttccgttcttcttcg > tcataa\ > cttaatgtttttatttaaaatacc,1},{tatttttgtaattcaatactgcttcttctt,11},{\ > tatttttgtaattcaatactgcttcttcttgcctttgcggaatttggccttttaaa,12},{ > aaattttccggtttaaggcgtttccgttctt,121},{ttcttgcctttgcgg,1211},{\ > aaattttccggtttaaggcgtttccgttcttcttcgtca,122},{actgcttcttcttgcctttgcggaat > ttg,\ > 1221},{tatttttgtaattcaatactgcttcttcttgcctttgcggaatttggccttttaaaagtatttat > cg,13}\ > ,{gctatttat,131},{gctatttatgaaaattttccggtttaaggcg,132},{gcggaatttggccttt > taaa,\ > 1321},{aaattttcc,13211},{\ > gctatttatgaaaattttccggtttaaggcgtttccgttcttcttcgtcataa,133},{\ > aatactgcttcttcttgcctttgcggaatttggccttttaaaagt,1331},{tgaaaattttccggttt,1 > 3311}} > > and it does the job, but I am wondering if it can be done by > Sort[list,func] where func would tell Sort to do the sorting of the > pairs by the second elements. I tried from Ted Érsek's excellent page > http://www.verbeia.com/mathematica/tips/HTMLLinks/Tricks_P-Z_22.html > > Sort[collectedDnaBin,#1[[2]] < #2[[2]] &] but that does not want to do > the trick for me. The Mapping of RoteteLeft and RotateRight, which is > a generalization of my naive approach above, he shows on the same page, > does it, but I do not know how to put that into Sort as second > argument. Maybe I should not, but rather to write a mySort function > based upon his idea... ? > > In general, if I have a list whose elements are m-member lists, like > {{a1,...,am},...,{z1,...,zm}}, then my question is what is the > best/fastest way to sort it on the k-th elements of the members where > 1<=k<=m and ak,...,zk are arbitrary objects - in my case Strings - and > use as little memory for it as possible. > > > Thanks ahead, > > János > ---------------------------------------------- > Trying to argue with a politician is like lifting up the head of a > corpse. > (S. Lem: His Master Voice) >