# My Java Solution with Explaination

• My idea is simple. I am using bubble sort and my own compare function which implements lexicographic comparison of two numbers by finding out no of digits of each number.

``````public String largestNumber(int[] nums) {
int n;
for(n=0;n<nums.length;n++)
{
if(nums[n]!=0)
break;
}
if(n==nums.length)
return new String("0");
for(int i=0;i<nums.length;i++)
{
for(int j=i+1;j<nums.length;j++)
{
if(compare(nums[i],nums[j]))
{
swap(nums,i,j);
}
}
}
StringBuilder s = new StringBuilder();
for(int i=0;i<nums.length;i++)
{
s.append(nums[i]);
}
return s.toString();
}
public void swap(int[] nums, int i, int j)
{
nums[i]=nums[i]^nums[j];
nums[j]=nums[i]^nums[j];
nums[i]=nums[i]^nums[j];
}
public boolean compare(int a, int b)
{
if(a == 0 && b!=0)
return true;
else if(a!=0 && b==0)
return false;
int i = (int)Math.log10(a)+1, j = (int)Math.log10(b)+1;
if(i==j && a<b)
return true;
else if(i==j && a>b)
return false;
else
return (a*Math.pow(10,j)+b) < (b*Math.pow(10,i)+a);
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.