Click here to Skip to main content
15,437,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
mport java.util.*;
public class sort1 {
	public static void main(String args[])
		int num,sort=0;
		System.out.println("enter a no.\n");
		Scanner s=new Scanner(;
		for(int i=0;i<=9;i++)
			int temp=num;
				int d=temp%10;


What I have tried:

i'm trying the above code which is working fine but it doesn't include zero.I want the zero as 2nd digit.
Updated 20-Aug-18 1:26am

If you mean "I want a zero in the least significant digit position" then try this:
int digits = log10(num) + 2;
sort = 0;
int temp = num;
while (digits-- > 0)
    int d = temp % 10;
    temp /= 10;
    sort = sort * 10 + d;
If you don't, then you will need to explain in a lot better detail!
Share this answer
Member 13954890 20-Aug-18 12:16pm     CRLF
It doesn't sort the digits in ascending order.I want to sort all the digits of a no. in ascending order.(except 0 which should always occupy the 2nd position such that the length of the no. remains same) e.g. input: no.=692204 output: 220469 input: no.=790402 output: 200479
OriginalGriff 20-Aug-18 12:32pm     CRLF
That's different: the easiest way to do that is to allocate an array big enough to hold each digit in a separate element (int or even just byte) then separate out each digit into a separate element. (You can get the number of elements your need with log10(yourNumber) + 1, that's what log10 effectively does). You can then sort the individual digits in the array, and print them each from there. The more complex method is to do the sorting "in the integer" but to be honest that's a pain in the ass to do. For the number of digits in an integer, I wouldn't bother.
Member 13954890 20-Aug-18 14:47pm    
Sorry! I'm not getting you.Will you please help me with code? Thanking you in advance.
OriginalGriff 20-Aug-18 15:02pm     CRLF
No, it's your homework not mine! Any way you know everything you need to do it. You know how to extract each digit separately, I know that. And you know how to use arrays, and how to sort, don't you?
There is a difficulty in printing a number with one significant zero, especially as you do not know if there is a zero in the number to begin with, and you do not know its length. The easiest way to do this would be to read the number as a string and than split it into characters and sort the characters in alphabetic order.
Share this answer
Member 13954890 20-Aug-18 14:19pm     CRLF
hey! I want to sort it in descending order.Since i'm using primitive datatype i can't use sort(arr.Collections.reversedorder).Then what is the alternative?Here is my code: import java.lang.*; import java.util.*; public class sort_string { public static void main(String[] args) { //String result = ""; System.out.println("enter string\n"); Scanner kbd = new Scanner(; String input = kbd.nextLine(); char[] arr=input.toCharArray(); Arrays.sort(arr); String sorted=new String(arr); System.out.println(sorted); } } the above code is working fine but it takes 0 in the first position.I want something like that-> input:936703 output:303679 so that the length of a no. remains same.
Richard MacCutchan 21-Aug-18 3:33am    
You will need to write your own method to handle that case. Basically, just sort the digits and check if the first character is '0'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900