Find an index to swap and create second permutation
for ( var j = next_pos ; j < n ; j ++ ) { if ( arr [ j ] [ 0 ] == arr [ j - 1 ] [ 0 ] ) { [ arr [ j ] , arr [ j - 1 ] ] = [ arr [ j - 1 ] , arr [ j ] ] ; next_pos = j + 1 ; break ; } } }