#New to comparators

10 messages · Page 1 of 1 (latest)

scarlet jasper
#

Good evening folks!
I'm quite new to java and right now I need a comparator for my code (Comparator<String>).
I want to create a comparator that compares two strings. It want it to work like that:

  • if the string is "test", "test" must be first
  • sort by alphabetical order after
stable sigilBOT
#

This post has been reserved for your question.

Hey @scarlet jasper! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

scarlet jasper
#

I think I worked it out:

return new Comparator<Category>() {
            @Override
            public int compare(Category o1, Category o2) {
                if (o1.getName().equals("General")) return 0;
                if (o2.getName().equals("General")) return 1;
                else {
                    return String.CASE_INSENSITIVE_ORDER.compare(o1.getName(), o2.getName());
                }
                
            }
        };
true onyx
#

that tests for "General"?

scarlet jasper
#

I fixed it another way

#

I don't need help anymore

#

thanks though

stable sigilBOT
# scarlet jasper thanks though

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.