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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:05:56+00:00 2026-06-14T16:05:56+00:00

I have a range of objects which needs to be ordered with a rule.

  • 0

I have a range of objects which needs to be ordered with a rule. But I need to be able to switch the rules however I have a limited set of ordering rules. Which data structure would be the best choice for that?

As an example I have this class:

class Test {
    public final int amount;
    public final int cost;
    public final String name;
    public final int whatever;
    // ...

    // TODO: add a constructor to set the fields :-)
}

How can I store those fields to order them by amount, cost, name or whatever. But just one of that rules.

I could imagine to use an ArrayList or a HashSet where I call the sort function with a custom Comparator. But I cannot imagine that this is efficiency. I think this is important on a mobile device. What is a better way to achieve 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-14T16:05:57+00:00Added an answer on June 14, 2026 at 4:05 pm

    You can not use an Set for sorting, as it does not have any order. How every the concept of having List and custom Comparator<T> is reasonable.

    You should go with that solution and do not care about performance at this point. If you will be not satisfied from gained result then try to came up with better solution.

    The best solution is reading data from storage in proper order. I do not know how your app store that structure. Therefore I can not help you with that. But implement the comparable solution and you will see that is not so bad.

    What is important on mobile device is memory usage. If your application will use lot of those sorting operation you could create the Comparators as enums so they will be loaded only once and in addition can simplify the code

    private enum  TestComparator implements Comparator<Test> {
     BY_NAME {
    
        @Override
        public int compare(Test o1, Test o2) {
    
           //We validate first against null
    
           return o1n.name.compareTo(o2.name);
    
        }               
      }
     BY_WHATEVER{
    
        @Override
        public int compare(Test o1, Test o2) {
    
           //We validate first against null
          return (o1.whatever<o2.whatever ? -1 : (o1.whatever==o2.whatever ? 0 : 1));
        }               
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a gql query against a set of some objects which
I have the following macro which needs to loop though an Excel-2007 table. The
I essentially have a bunch of data objects which map timestamps in milliseconds to
I need to store about 100 thousands of objects representing users. Those users have
I have a multimap with Note objects from which I want to remove only
I have a coworker who writes unit tests for objects which fill their fields
I have a NSFetchedResultsController which is fetching objects from a NSManagedObjectContext . I'm using
I have this query: checkins = CheckinAct.objects.filter(time__range=[start, end], location=checkin.location) Which works great for telling
In my current project, I have quite a few objects I need to persist
I have some code which deals with various std::list objects, and I am currently

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.