MATLAB: Use sorted variable to reorder rows of a matrix

indexMATLABmatricesmatrix manipulationrowssortsum

I am attempting to reorder the rows of a matrix from greatest number of nonzero elements to least. I calculate the number of zeros per row and then sort that variable to get the correct order, but I am having tourble using my sorted variable to reorder the original matrix. I have posted what I have so far below
Index_matrix = [42 33 27 22 17 12 7 4 2 1 0
43 34 26 21 16 11 6 3 1 0 0
44 35 28 22 17 12 7 4 2 1 0
45 36 29 23 18 13 8 2 1 0 0
46 37 30 24 19 14 9 3 1 0 0
47 38 31 25 20 15 10 5 2 1 0
48 39 32 26 21 16 11 6 3 1 0
49 40 33 27 22 17 12 7 4 2 1
50 41 31 25 20 15 10 5 2 1 0];
test = sum(Index_matrix==0, 2);
test2 = sort(test,1);
index_matrix = sort(Index_matrix, test2);

Best Answer

How about the following solution?
test = sum(Index_matrix==0, 2);
[~, order] = sort(test);
Index_matrix = Index_matrix(order,:);
The result is like:
>> Index_matrix
Index_matrix =
49 40 33 27 22 17 12 7 4 2 1
42 33 27 22 17 12 7 4 2 1 0
44 35 28 22 17 12 7 4 2 1 0
47 38 31 25 20 15 10 5 2 1 0
48 39 32 26 21 16 11 6 3 1 0
50 41 31 25 20 15 10 5 2 1 0
43 34 26 21 16 11 6 3 1 0 0
45 36 29 23 18 13 8 2 1 0 0
46 37 30 24 19 14 9 3 1 0 0