• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Sorting hexidecimal number strings in an ArrayList

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following code:

The output is:
[30, 3A, 8, FF]

According to the book: Oracle Certified Associate Java SE 8 Programmer I Study Guide, by Jeanne Boyarsky and Scott Selikoff, the answer key for Chapter 3 Review Question 25 says, "Remember that numbers sort before letters and strings sort alphabetically. This makes 30 come before 8".

From my current level of understanding, that goes completely against the grain with how integer arrays are sorted. Even with files stored on your computer, the file names that begin with numbers are listed in order before the file names that begin with letters are alphabetically sorted in order (see my attached image).

All of that being said, how is the sorted list not ["8","30","3A","FF"] instead?
AlphabeticNumericSorting.PNG
[Thumbnail for AlphabeticNumericSorting.PNG]
 
Marshal
Posts: 76395
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Peterson wrote:. . . how integer arrays . . .

What integer arrays? You haven't get an integer array, but a String array. Never mind how integers are sorted, the book is correct; because '3' has a lower ASCII value than '8', both "30" and "3F" sort before "8".
Please avoid screenshots, but that shows '3' coming before '4' and numbers coming before letters. It also shows longer names before shorter names, so you have a different sorting order than you would get in Java®.
 
Saloon Keeper
Posts: 14264
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see how the example from your file system contradicts what the book says.

Anyway, while the integer 30 means "thirty", the string "30" means "first the character '3' and then the character '0'". Strings are compared on a character-by-character basis. The character '3' comes before the character '8', so the string "30" comes before the string "8".
 
Rancher
Posts: 4885
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding a Comparator to the call to the sort method. Have the Comparator convert the Strings to int values for comparing.
 
Bartender
Posts: 5061
188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed, and a very handy way to convert a hexadecimal String into an int is by using:

A suitable Comparator is now just one line away.
 
Campbell Ritchie
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we convert that Comparator to a λ?
 
Piet Souris
Bartender
Posts: 5061
188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but lets here from OP whether he knows about Comparators.
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another perspective of looking at the same problem :
 
Campbell Ritchie
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't a List have a sort() method of its own nowadays?
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic