To sort a matrix or dataframe, you need to use the order() function as follows, using the vectors on which you want to sort (e.g., here it's sorting first on the second column and then the first column of the matrix):
mat=mat[order(mat[,2],mat[,1]),]
Here's a simple wrapper function where ... indicates the vectors on which to do the sorting:
dsort=function(obj,...){ return(obj[order(...),])}
Last modified: 12/13/08.