sorts and extractmat for random forests