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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:21:36+00:00 2026-05-27T14:21:36+00:00

Is there a better way to write this comparator? I have an equivalent of

  • 0

Is there a better way to write this comparator? I have an equivalent of multidimensional array where columns are Object‘s. The actual objects are String and BigDecimal 99% of the time. I am sorting “rows” on a given column index.

I want to avoid instanceof.

protected static class MyComparator implements Comparator<DataRow> {
    private int idx;

    public MyComparator(int idx) {
        this.idx = idx;
    }

    @Override
    public int compare(DataRow r1, DataRow r2) {
        Object o1 = r1.getColumns()[idx];   
        Object o2 = r2.getColumns()[idx];
        if (o1 instanceof String){
            return ((String)o1).compareTo((String)o2);
        }else if (o1 instanceof BigDecimal){
            return ((BigDecimal)o1).compareTo((BigDecimal)o2);
        }else{
            throw new UnsupportedOperationException("comparison cannot be performed");
        }
    }
  • 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-05-27T14:21:37+00:00Added an answer on May 27, 2026 at 2:21 pm

    I think since you only depend on the type Comparable you could rewrite it as:

    public int compare(DataRow r1, DataRow r2) {
            Comparable o1 = (Comparable) r1.getColumns()[idx];   
            Comparable o2 = (Comparable) r2.getColumns()[idx];
            return o1.compareTo(o2);
    }
    

    If you carefully populate the table you shouldn’t face the UnsupportedOperationException situation.

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

Sidebar

Related Questions

i have 10 moving objects (UIImageView), is there a better way to write this
Is there a better way to write this I have A Condition which previous
Is there a better way to write this string q = Request.QueryString[q] ?? string.Empty;
In Cakephp is there a better way to write this: $unread_orders = $this->Order->find('all', array('conditions'
Is there a better way to write this code? I want to show a
is there any way to write this foreach in linq or another better way,
Is there a better simplified way to write this query. My logic is if
Is there a better way to write this code? i tried function(){}(); but i
Is there a better way to write this SQL query? SELECT *, (SELECT TOP
Like the question says is there a better way to write this: SELECT clients_lists.*,

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.