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

  • Home
  • SEARCH
  • 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 8377193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:36:23+00:00 2026-06-09T15:36:23+00:00

I’ve got a simple java pojo which looks like this: class MyClass { public

  • 0

I’ve got a simple java pojo which looks like this:

class MyClass
{
  public String getGroup();
  public String getTitle();
}

Now what I want to do is to primarily sort a list of MyClass pojo by the values returned by the getTitle() method. Easy going though using my own comparator. However, what I want is that instances with the same value returned by getGroup() being followed by each other. Now what I did was something like

.. compare(MyClass c1, MyClass c2)
{
  if (c1.getGroup().compareTo(c2.getGroup()) == 0)
  {
    return c1.getTitle().compareTo(c2.getTitle());
  }
  return c1.getGroup().compareTo(c2.getGroup());
}

However, the issue of this code is that it is no longer primarily sorted by the title because I do first compare the content of the groups, not the title, so a group starting with “B” would come before a group starting with “C” eventhough its title may come first.. what’s the proper way to primarily sort by title but make sure groups are “groupped” together as well?

Sample data:

MyClass 1 (group = "A", title="5")
MyClass 2 (group = "B", title="9")
MyClass 3 (group = "B", title="1")

Using my previous code would end up in

MyClass 1 (group = "A", title="5")
MyClass 3 (group = "B", title="1")
MyClass 2 (group = "B", title="9")

-> sort by group, then sort by title

But I want

MyClass 3 (group = "B", title="1")
MyClass 2 (group = "B", title="9")
MyClass 1 (group = "A", title="5")

-> sort by title but make sure each equal group follows each other that’s why still MyClass 1 with title “5” comes after MyClass 2 with title “9”…

  • 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-09T15:36:24+00:00Added an answer on June 9, 2026 at 3:36 pm

    Can’t be done with a Comparator. You need to run through the sorted titles, then for each unvisited title sort the corresponding groups. Here’s some sample code that sorts like the way you wanted.

    public class MyClass {
        private String  group;
        private String  title;
    
        public MyClass(String g, String t) {
            group=g;
            title=t;
        }
    
        static Comparator<MyClass>  TITLE_COMPARATOR    = 
                new Comparator<MyClass>() {
                        @Override
                        public int compare(MyClass c1, MyClass c2) {
                                return c1.title.compareTo(c2.title);
                        }
                };
        static Comparator<MyClass>  GROUP_COMPARATOR    = new Comparator<MyClass>() {
                        @Override
                        public int compare(MyClass c1, MyClass c2) {
                                return c1.group.compareTo(c2.group);
                        }
                };
    
        public static List<MyClass> sublist(List<MyClass> list, String group) {
                ArrayList<MyClass> ret = new ArrayList<MyClass>();
                for (MyClass mc : list)
                    if (mc.group.equals(group))
                        ret.add(mc);
                return ret;
            }
    
    public static void main(String[] argv) {
        ArrayList<MyClass> sorted = new ArrayList<MyClass>();
    
        ArrayList<MyClass> list = new ArrayList<MyClass>();
        list.add(new MyClass("A", "5"));
        list.add(new MyClass("B", "9"));
        list.add(new MyClass("B", "1"));
        Collections.sort(list, TITLE_COMPARATOR);
        Hashtable<String, Boolean> visited = new Hashtable<String, Boolean>();
        for (MyClass mc : list) {
            if (visited.get(mc.group) == null) {
                List<MyClass> sublist = sublist(list, mc.group);
                Collections.sort(sublist, GROUP_COMPARATOR);
                sorted.addAll(sublist);
                visited.put(mc.group, Boolean.TRUE);
            }
        }
    
        for (MyClass mc : sorted)
            System.out.println(mc.group + " " + mc.title);
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
i got an object with contents of html markup in it, for example: string
Does anyone know how can I replace this 2 symbol below from the string

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.