• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sorting decimal and hexadecimal values with sort

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question from OCA programmer guide by Jeanne Boyarsky and Scott selikoff


this is the output:
30
3A
8
FF

How does this sorting take place.


 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay , i got this now. These are just string values , they are not converted to Hexadecimal.

String sorts in alphabetical order, Numbers sort before letters and uppercase sorts before lowercase
from OCA programmer guide by Jeanne and Scott.

since 3 (of 30) and 3 (of 3A) are smaller numbers than 8 they sort before it and FF , comes last.

its confusing with 3A.

Have I understood this right ???


 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:okay , i got this now. These are just string values , they are not converted to Hexadecimal.

You are absolutely spot-on! The reference variable hex is of type List<String>, so this list contains nothing but strings. And string values are sorted in alphabetical order and that's different from sorting numbers. Try sorting these two lists and you'll see the difference

Ramya Subraamanian wrote:its confusing with 3A.

Have I understood this right ???

Sorting string values can indeed be confusing, certainly when upper case, lower case and numbers are combined like in this example with "30" and "3A". But once you know the sort order, it's very easy. When you are sorting string values, this is the sort order (also mentioned in the study guide):
1/ numbers (0 - 9)
2/ uppercase letters (A - Z)
3/ lowercase letters (a - z)

So "30" is sorted before "3A". Why? The first character of both strings is the same ("3"). Then the next character is compared and "0" is sorted before "A", because as you know numbers are sorted before upper case letters.

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:These are just string values , they are not converted to Hexadecimal.

Let's turn this one into a little assignment Write a little code snippet that converts the list hex into a List of Integers, sorts this list and prints each element of this list.

If you have written this small application, the output should be something like: 8 48 58 255 .

Happy coding!
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this is the output:
l2-->30
l2-->8
l2-->58
l2-->255
Sorted l2-->8
Sorted l2-->30
Sorted l2-->58
Sorted l2-->255


this took more time than i thought. finding about conversion of string to hex took more time...hope this is what you expected.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:this is the output:
l2-->30
l2-->8
l2-->58
l2-->255
Sorted l2-->8
Sorted l2-->30
Sorted l2-->58
Sorted l2-->255

That's indeed the expected output. Have a cow for your effort!

Ramya Subraamanian wrote:this took more time than i thought. finding about conversion of string to hex took more time...hope this is what you expected.

You have the expected result, so that's already very good But some improvements could/should be made to have not only the expected result but high quality code as well (which will be easier to read, maintain, test, and so on):
  • always try to use the naming conventions (e.g. variable and method names should start with a lower case letter and camelCase notation)
  • always use meaningful and appropriate variable and method names (e.g. mymeth, Inhex, l, l2, and l5 are very poor names)
  • For converting hex to decimal, you should have a look at Integer.parseInt(String, int). It will definitely simplify your code significantly.


  • Hope it helps!
    Kind regards,
    Roel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic