Skip to content
Prev 378901 / 398502 Next

Sorting vector based on pairs of comparisons

Hi Pedro,
This looks too simple to me, but it seems to work:

swap<-function(x,i1,i2) {
 tmp<-x[i1]
 x[i1]<-x[i2]
 x[i2]<-tmp
 return(x)
}
mpo<-function(x) {
 L<-unique(as.vector(x))
 for(i in 1:nrow(x)) {
  i1<-which(L==x[i,1])
  i2<-which(L==x[i,2])
  if(i2<i1) L<-swap(L,i1,i2)
 }
 return(L)
}
mpo(matComp)

Jim
On Thu, Mar 14, 2019 at 10:30 PM Pedro Conte de Barros <pbarros at ualg.pt> wrote: