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 8175317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:44:25+00:00 2026-06-06T22:44:25+00:00

The issue is to sort a huge collection of POJO objects class Entity{ String

  • 0

The issue is to sort a huge collection of POJO objects

class Entity{
    String key1;
    String key2;
    String key3;
    String key4;
}

in alphabetical order by all of fields subsequently. That means that first we are sorting by key1, then by key2 and etc. Any of that key can be null. The question is the simplest way to do that.

  • 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-06T22:44:27+00:00Added an answer on June 6, 2026 at 10:44 pm

    A good way to do append all keys together and then use a Comparator<Entity> –

    // all the keys of an entity are appended and than compared with other entity
    int compare(Entity e1, Entity e2){
      return appendAndHandleNull(e1.key1, e1.key2, e1.key3).compareTo(appendAndHandleNull(e2.key1, e2.key2, e2.key3));
    }
    
    
    
    /** 
     * method to get all keys of an entity in appended form
     */
    private static final String appendAndHandleNull(String list...){
       StringBuilder result = new StringBuilder ();
       for(String s : list){
          result.append(s!=null?s:"").append(" ");//note: a space is appended after each key
       }
       return result.toString();
    }
    

    What i am doing here is….. all the keys of an entity are appended together in the order in which comparison need to be done, and then compared with the other entity.

    you might also need to trim each value before appending.

    EDITED:
    It is also necessary to seperate each key by space for code to work properly.

    Code fixed above. Thanks @Thilo for pointing it.

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

Sidebar

Related Questions

I have a capped collection and I know I need to issue: find().sort({$natural:-1}) to
I am in trouble to sort the issue of playing the you tube video
I have an issue when I sort desc/asc on a column in a database
My issue is very similar to this one: XSLT: use parameters in xls:sort attributes
I have one very tricky issue that I am not able to sort out
In the following code, I'm having some sort of issue getting my .yaml file
I have see solutions to this sort of issue with 1:N, but they dont
I'm working on a basic Issue Management System in order to learn ASP.NET MVC.
I have an issue to sort a SQL results and really would appreciate your
I am trying to solve an issue sort of similar to what was done

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.