Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:54:34+00:00 2026-06-09T02:54:34+00:00

Hello stackoverflow community! I am new to these forums and also fairly new to

  • 0

Hello stackoverflow community!
I am new to these forums and also fairly new to java and android programming–which happen to be the objects of my question–so sorry in advance for any blunders!

My issue is sorting. I am looking for a method to sort objects based on a field that I choose (not sorting based on the first field, then the next, etc. exemplified by comparator chaining). I believe I’ve found the solution to my problem:

https://stackoverflow.com/a/5113108/1549672

but I am having trouble actually getting this to work. I have a suspicion that I’m probably missing something due to my lack of java experience, so any help is welcome!

Here is what I am trying:

As my class-

public class ItemLocation {
String title;
int id;
}

As my function-

public void sort(final String field, List<ItemLocation> itemLocationList) {
    Collections.sort(itemLocationList, new Comparator<ItemLocation>() {
        @Override
        public int compare(ItemLocation o1, ItemLocation o2) {
            if(field.equals("title")) {
                return o1.title.compareTo(o2.title);
            } else if(field.equals("id")) {
                return Integer.valueOf(o1.id).compareTo(o2.id);
            }
            return 0;
        }
    });
}

using these, could someone possibly give an example of using this method? I attempted to fill an ArrayList and sort it, but to no avail.

Thanks for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:54:36+00:00Added an answer on June 9, 2026 at 2:54 am

    You should not return 0 from the Comparator.compare method if they are not equal. It’s “okey” by the contract, but not exactly encouraged, from the API documentation:

    It is generally the case, but not strictly required that (compare(x,
    y)==0) == (x.equals(y)). Generally speaking, any comparator that
    violates this condition should clearly indicate this fact. The
    recommended language is “Note: this comparator imposes orderings that
    are inconsistent with equals.”


    In my opinion you should return a specific Comparator for each field instead:

    Comparator<ItemLocation> titleComparator = new Comparator<ItemLocation>() {
        @Override
        public int compare(ItemLocation o1, ItemLocation o2) {
            return o1.title.compareTo(o2.title);
        }
    }
    
    Comparator<ItemLocation> idComparator = new Comparator<ItemLocation>() {
        @Override
        public int compare(ItemLocation o1, ItemLocation o2) {
            return Integer.valueOf(o1.id).compareTo(o2.id);
        }
    }
    
    public void sort(final String field, List<ItemLocation> itemLocationList) {
    
        final Comparator<ItemLocation> comparator;
    
        if(field.equals("title")) {
            comparator = titleComparator;
        } else if (field.equals("id")) {
            comparator = idComparator;
        } else {
            throw new IllegalArgumentException("Comparator not found for " + field);
        }
    
        Collections.sort(itemLocationList, comparator);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello StackOverflow Users, I am new to android and trying to develop a game
Hello stackoverflow pals.I am a new rails learner. My question is: I am listing
Hello again stackoverflow, I have a question concerning List of Objects. I have tried
Hello StackOverflow community! I started to learn Node.js recently, and decided to implement a
Hello stackoverflow community ! I am trying to figure out how to architect my
Hello StackOverflow community, Using Google App Engine, I wrote a keyToSha256() method within a
I want to say hello to the stackoverflow community. I've just started using knockout
Hello again, stackoverflow community! I'm working on writing a simple blog system in Rails,
Hello stackoverflow people! I've got a problem which I haven't been able to solve
Hello StackOverflow community, The air.swf file referenced here: http://livedocs.adobe.com/flex/3/html/help.html?content=distributing_apps_3.html used to launch AIR applications

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.