Sorting In an array

How to sort Element in array
For Example arr[] = {“Code1”,“Code10”,“Code2”,“Code20”,code30};
output should be code1,code2,code10,code20,code30
but when i use arrays.sort in java it gives the output code1,code10,code2,code20;

What you can do is make a string array - store all the elements in it.

Now, look each element of the string starts with “code” and then the number.

What you can do is skip the first four characters and parse the rest of the string as an integer or long depending on your situation.

Then just do the regular sorting with those number values.

Hope this helps!!

The reason java’s sort gives that order is that, it compares char by char.

So, “10” is actually seen as ‘1’ and ‘0’. And ‘1’ <‘2’ , so code 1,10,100,1000 etc. get placed before code 2.

Check the syntax, if its possible to define a custom operator using which this function sorts. You can define it such that it compares by the last 2-3 digits, which are numbers.

If thats not possible, I am afraid you will have to manually sort it.

1 Like

Here you go

public class CodeChef {

public static void main(String[] args)
{
	String[] array = {"code56","code53", "code1536","code2","code23"};
	
	int[] array1 = new int[array.length];
	
	String number = "";
	for(int i = 0;i<array.length;i++)
	{
		number = "";
		for(int j = 4;j<array[i].length();j++)
		{
			number+=array[i].charAt(j);
		}
		array1[i] = Integer.parseInt(number);
	}
	
	int temp = 0;
	String tempString = "";
	for(int i = 0;i<array1.length;i++)
	{
		for(int j = i+1;j<array.length;j++)
		{
			if(array1[i]>array1[j])
			{
				temp = array1[i];
				array1[i] = array1[j];
				array1[j] = temp;
				tempString = array[i];
				array[i] = array[j];
				array[j] = tempString;
			}
		}
	}
	
	for(int i = 0; i<array.length;i++)
		System.out.print(array[i] + " ");
}

}

If its string part is same as code for all values then u can choose the ascii values to sort it out, take the sum of all and sort from low to high, or else u will need to input like these, insert a zero like code10, code01, code02 like that :slight_smile:

@vijju123 it’s actually possible to sort the above array in java

Its always possible to sort in O(N^2). I am talking about sorting using the in built function :p.

oh I’m sorry I misunderstood it

lol this question is getting so many views

Because mostly O(N^2) gives TLE due to large input. AFAIk, in built ones always sort in O(NlogN), and thats why we prefer to use via in built functions. Saves a lot of time :smiley:

2 Likes

oh wow I didn’t know that, thank you for the information

what if we need to sort string part also for example
we are combining name and roll number
drjaat19/kunnu120/vijju123

Then you need to have a condition get all the characters until it sees an integer value then parse that index to the last index into the integer.

for ex - drjaat19

it will go like this - is ‘d’ a integer value(0-9)? no

go next is ‘r’ a integer value? no, go next. is ‘j’ a charcter nope then go next then it will go like this until it reach 1 then parse the string from index 6 to last index.

1 Like