question on how to re-order matrix

Welcome to the community!

A reproducible example is very useful, as it helps people to help you. You don't need to know tidyverse for it (believe me, I don't know either). Please take a look at this great post by Andres:

I'll assume a random matrix to get you started, but it's not a proper thing to do. What you have is similar to a distance matrix, and I failed to anything as such. Still, I hope the following will give you some idea to reorder the rows and columns of a matrix.

set.seed(seed = 31782)

dataset <- matrix(data = rnorm(n = 16),
                  nrow = 4,
                  ncol = 4,
                  dimnames = list(c("ind2", "ind4", "ind1", "ind3"),
                                  c("ind1", "ind4", "ind3", "ind2")))
dataset
#>            ind1       ind4       ind3        ind2
#> ind2 -0.8865502 -0.6995333 -1.6436619  0.06188118
#> ind4  0.3434197 -0.4407043 -0.3167084  1.03358285
#> ind1 -0.1445536 -0.4691033  0.4753363  0.04370875
#> ind3 -0.3512611  1.1011608  0.1883204 -1.24454409

dataset[order(row.names(x = dataset)), order(colnames(x = dataset))]
#>            ind1        ind2       ind3       ind4
#> ind1 -0.1445536  0.04370875  0.4753363 -0.4691033
#> ind2 -0.8865502  0.06188118 -1.6436619 -0.6995333
#> ind3 -0.3512611 -1.24454409  0.1883204  1.1011608
#> ind4  0.3434197  1.03358285 -0.3167084 -0.4407043

Created on 2019-05-27 by the reprex package (v0.3.0)

Good luck!