Compare lines and sort them by compatibility

If anybody is going to be able to help we will need some idea of what you are trying to do. At the very least we will need a reproducible example (reprex)

with a decent amount of sample data. A handy way to supply sample data is to use the dput() function. See ?dput. If you have a very large data set then something like head(dput(myfile), 100) will likely supply enough data for us to work with.