Re: Sorting a list of pairs on the second elements
- To: mathgroup at smc.vnet.net
- Subject: [mg51379] Re: Sorting a list of pairs on the second elements
- From: p-valko at tamu.edu (Peter Valko)
- Date: Fri, 15 Oct 2004 02:47:42 -0400 (EDT)
- References: <cklmi7$f1o$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
János, Using a smaller example: collectedDnaBin = {{"a", "2"}, {"b", "1"}}; ord[p1_, p2_] := OrderedQ[{p1[[2]], p2[[2]]}]; Sort[collectedDnaBin, ord] will result in {{"b", "1"}, {"a", "2"}} Regards (Udv) Peter János <janos.lobb at yale.edu> wrote in message 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)