Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Method to alphebetize Method objects consistently misses those starting with 'A'!

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm attempting to write a method which returns an alphabetical list of Method objects given some arbitrary list of method objects. I cannot for my life figure out why my method keeps missing Method objects starting with 'A'. After methods starting with 'A', everything seems to work fine! Any feedback is much appreciated.

Here's my method to alphabetize method objects:


I'm running my method using a class with the following method names:



And I consistently see the Method BothNegNums() is first! After this, the list is correctly alphabetized (item 2 is 'absNonNeg', 'boo', etc).

I must be missing something simple since the behavior is correct after the 0th item. Thanks for taking a look.
 
Marshal
Posts: 72479
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What methods beginning A? I can't see any in your post. There are methods starting a, however. Method names should always start with lower‑case letters, anyway.
Remember, too, that 'Z' in ASCII comes seven places before 'a'.
 
Master Rancher
Posts: 4256
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I consistently see the Method BothNegNums() is first!  


Do you know the values of the characters?  Try this:
 
Simon McNamara
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For crying out loud.

Somehow I forgot to account for the fact of upper case letters, even while I have a method with an upper case letter...!

If I change my comparison to the following, the method behaves correctly:

FROM THIS:


TO THIS:


Thank guys,
 
Campbell Ritchie
Marshal
Posts: 72479
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but the correct solution is to avoid starting method names with upper case letters.
 
Simon McNamara
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed, but I can't control which methods a client puts into their class. If they call my code, it'll still execute in the correct order despite their deplorable java styling.
 
Sheriff
Posts: 16132
268
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've written way more code than you really need to. There are a number of ways you can do this in a single line of code. Google for java sort with comparator equalsignorecase and read through some of the results.
 
Campbell Ritchie
Marshal
Posts: 72479
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alternatively, you are sorting the names of methods as Strings, and the String class supplies such a Comparator ready‑made.
 
Simon McNamara
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the feedback. Yes, I suppose it is rather verbose. I'll check out that link. Thanks for your help.
 
Campbell Ritchie
Marshal
Posts: 72479
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether you use verbose code depends on your stage of life, at least in part. Students might be told to implement their own algorithms as an exercise, but later, in production, the same people will use ready‑made algorithms because they have been tested widely and designed to cope with corner cases....and not know what differences there are between the versions.
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic