Sorting a list of pairs on the second elements
- To: mathgroup at smc.vnet.net
- Subject: [mg51318] Sorting a list of pairs on the second elements
- From: János <janos.lobb at yale.edu>
- Date: Thu, 14 Oct 2004 06:36:13 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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)