text: Sort prime numbers of an array in descending order | Javascript implementation of the approach ; false here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function that sorts all the prime numbers from the array in descending ; this vector will contain prime numbers to sort ; if the element is prime ; update the array elements ; Driver code ; print the results .
code: var prime = Array ( 100005 ) . fill ( true ) ; function SieveOfEratosthenes ( n ) { prime [ 1 ] = false ; for ( var p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( var i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } } function sortPrimes ( arr , n ) { SieveOfEratosthenes ( 100005 ) ; var v = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) v . push ( arr [ i ] ) ; } v . sort ( ( a , b ) => b - a ) var j = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) arr [ i ] = v [ j ++ ] ; } } var arr = [ 4 , 3 , 2 , 6 , 100 , 17 ] ; var n = arr . length ; sortPrimes ( arr , n ) ; for ( var i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; }