I just finished this task but then I'm confused how it works
Please someone explain the algorithm
Under discussion
Comments (9)
- Popular
- New
- Old
You must be signed in to leave a comment
Jurij Thmsn
10 May 2021, 13:56
You iterate through the numbers and sort them in ascending order and you iterate through the words sorting them in ascending order.
By just comparing numbers with numbers and words with words, you don't change the order between words and numbers.
What exactly confused you? maybe you want to share the piece of code which did.
+2
John
11 May 2021, 12:40
i mean the order of the sample input, lets say first input is 1 and then second is banana (both are Strings) and the algorithm compares the two of them. how come is that possible and what would be the less than and greater than between them
0
Jurij Thmsn
11 May 2021, 14:20
If you compare two Strings with e.g. the .compareTo() method, the Strings are compared lexicographically.
This means each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value.
The result is positive if the first string is lexicographically greater than the second string else the result would be negative.
You can check an ASCII table to know which character is smaller/greater.
you can also check the ASCII value of a character on your own by casting a char into a byte:
+1
John
12 May 2021, 01:15
So is that what made the comparison between String a = "Apple" and String b = "20" (just an example) possible?
0
Jurij Thmsn
12 May 2021, 12:31
yes, because as you can se in the ASCII table, every character has its own unicode. Letters as well as numbers or signs.
+2
John
12 May 2021, 13:14
Thank you!
0
ImDevin
13 May 2021, 23:37
way the solution is written, due to the nested for-loop & the if statements, it will only compare number to number & word to word.
0
John
14 May 2021, 03:17
but the input is aternately number and a word
0
Thomas
14 May 2021, 06:04
Inside your nested sort loops but before you compare two inputs you check if they are both strings or can be converted to a number.
a) both are Strings: sort according to the task conditions
b) both are numbers: sort - " -
c) they are of different types: do nothing and continue to the next iteration
0