Given an array **arr[]** of **N** non-negative integers, the task is to sort these integers according to the product of their digits.

**Examples:**

Input:arr[] = {12, 10, 102, 31, 15}

Output:10 102 12 31 15

10 -> 1 * 0 = 0

102 -> 1 * 0 * 2 = 0

12 -> 1 * 2 = 2

31 -> 3 * 1 = 3

15 -> 1 * 5 = 5

Input:arr[] = {12, 10}

Output:10 12

**Approach:** The idea is to store each element with its product of digits in a vector pair and then sort all the elements of the vector according to the digit products stored. Finally, print the elements in order.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the product ` `// of the digits of n ` `int` `productOfDigit(` `int` `n) ` `{ ` ` ` `int` `product = 1; ` ` ` `while` `(n > 0) { ` ` ` `product *= n % 10; ` ` ` `n = n / 10; ` ` ` `} ` ` ` `return` `product; ` `} ` ` ` `// Function to sort the array according to ` `// the product of the digits of elements ` `void` `sortArr(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Vector to store the digit product ` ` ` `// with respective elements ` ` ` `vector<pair<` `int` `, ` `int` `> > vp; ` ` ` ` ` `// Inserting digit product with elements ` ` ` `// in the vector pair ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `vp.push_back(make_pair(productOfDigit(arr[i]), arr[i])); ` ` ` `} ` ` ` ` ` `// Sort the vector, this will sort the pair ` ` ` `// according to the product of the digits ` ` ` `sort(vp.begin(), vp.end()); ` ` ` ` ` `// Print the sorted vector content ` ` ` `for` `(` `int` `i = 0; i < vp.size(); i++) ` ` ` `cout << vp[i].second << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 12, 10, 102, 31, 15 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `sortArr(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the product ` `# of the digits of n ` `def` `productOfDigit(n) : ` ` ` ` ` `product ` `=` `1` `; ` ` ` `while` `(n > ` `0` `) : ` ` ` `product ` `*` `=` `(n ` `%` `10` `); ` ` ` `n ` `=` `n ` `/` `/` `10` `; ` ` ` ` ` `return` `product; ` ` ` `# Function to sort the array according to ` `# the product of the digits of elements ` `def` `sortArr(arr, n) : ` ` ` ` ` `# Vector to store the digit product ` ` ` `# with respective elements ` ` ` `vp ` `=` `[]; ` ` ` ` ` `# Inserting digit product with elements ` ` ` `# in the vector pair ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `vp.append((productOfDigit(arr[i]), arr[i])); ` ` ` ` ` `# Sort the vector, this will sort the pair ` ` ` `# according to the product of the digits ` ` ` `vp.sort(); ` ` ` ` ` `# Print the sorted vector content ` ` ` `for` `i ` `in` `range` `(` `len` `(vp)) : ` ` ` `print` `(vp[i][` `1` `], end ` `=` `" "` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `12` `, ` `10` `, ` `102` `, ` `31` `, ` `15` `]; ` ` ` `n ` `=` `len` `(arr); ` ` ` ` ` `sortArr(arr, n); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

10 102 12 31 15

**Time Complexity:** O(nlogn)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sort the numbers according to their sum of digits
- Sort integers in array according to their distance from the element K
- Sort the array according to their cubes of each element
- Count of largest sized groups while grouping according to product of digits
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Count different numbers possible using all the digits their frequency times
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Check if product of first N natural numbers is divisible by their sum
- Print N numbers such that their product is a Perfect Cube
- Sort ugly numbers in an array at their relative positions
- Count of groups having largest size while grouping according to sum of its digits
- Find the size of largest group where groups are according to the xor of digits
- Sort an array according to the order defined by another array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.