I have a correlation matrix `data.corr` which consists of 163 companies, therefore the matrix dimensions are 163 rows and 163 columns.

I want to sort the highest 5 correlation values of each companny, so I wrote the following code:

```
COR<- matrix(nrow = 5, ncol = 163)
for(i in 1:163){COR[,i]<-tail(sort(data.corr[i,]),5)}

```

It works well, but the problem that it doesn't copy the rows and columns name from the original correlation matrix for each value. 

the row and column names are the company names.
here is a sample of the correlation matrix:

```
    head(data.corr)
X601288.SS X601988.SS X601998.SS X601818.SS X601939.SS X601398.SS
X601288.SS  1.0000000  0.7628263  0.6130694  0.7947062  0.7578003  0.7568238
X601988.SS  0.7628263  1.0000000  0.7280957  0.6925497  0.8402101  0.8409767
X601998.SS  0.6130694  0.7280957  1.0000000  0.6715793  0.7118446  0.6716997
X601818.SS  0.7947062  0.6925497  0.6715793  1.0000000  0.6825405  0.6471228
X601939.SS  0.7578003  0.8402101  0.7118446  0.6825405  1.0000000  0.8390544
X601398.SS  0.7568238  0.8409767  0.6716997  0.6471228  0.8390544  1.0000000

```

here a sample of the highest five correlations:

```
head(COR)
          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]
[1,] 0.7568238 0.7280957 0.6894561 0.6715793 0.7558052 0.7323083 0.7323083 0.6894561
[2,] 0.7578003 0.7628263 0.7118446 0.6825405 0.7578003 0.7568238 0.7472125 0.6956420
[3,] 0.7628263 0.8402101 0.7223088 0.6925497 0.8390544 0.8390544 0.7558052 0.7007705
[4,] 0.7947062 0.8409767 0.7280957 0.7947062 0.8402101 0.8409767 0.7618053 0.7618053
[5,] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000

```

any ideas how to sort the values with their matching column and row names?