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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:38:53+00:00 2026-06-06T20:38:53+00:00

I’m trying to sort an ArrayList by BigDecimal and coming up against a brickwall.

  • 0

I’m trying to sort an ArrayList by BigDecimal and coming up against a brickwall. I have a class that looks a little like this:

public class deal implements Comparable<deal>{

protected BigDecimal bdPrice;
protected int iQuantity;
protected String sDealType;
protected UUID dealUniqueID;
protected int dealID;
protected BigDecimal bdUnitPrice;

public deal(){
    bdPrice         = new BigDecimal("0");
    bdUnitPrice     = new BigDecimal("0");
    iQuantity       = 1;
    sDealType       = "Single item";
    dealUniqueID    = UUID.randomUUID();
    dealID          = 0;
}

private void setUnitPrice(){
    this.bdUnitPrice = this.bdPrice.divide(new BigDecimal(this.iQuantity));
}

public BigDecimal compareTo(deal oDeal) {
    // TODO Auto-generated method stub
    return bdUnitPrice.compareTo(oDeal.getUnitPrice());
}

public boolean equals(deal oDeal) {
    if (!(oDeal instanceof deal))
        return false;
    deal oD = (deal) oDeal;

    return this.bdUnitPrice.equals(oD.bdUnitPrice);
}
}

and my main Android Activity looks like this:

public class SupermarketDealsActivity extends Activity {
private ArrayAdapter<deal> itemAdapter;
private ListView lvDeals;
private ArrayList<deal> itemArray;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SetUpView();
}

private void SetUpView(){
lvDeals                     = (ListView)this.findViewById(R.id.listDeals);

    itemArray                   = new ArrayList<deal>();
    itemArray.clear();

    itemAdapter                 = new ArrayAdapter<deal>(this, android.R.layout.simple_list_item_1,itemArray);
    lvDeals.setAdapter(itemAdapter);
}

private void CreateADeal(int iQuantity, BigDecimal bdPrice) {
    deal oDeal                  = new deal();

    oDeal.setQuantity(iQuantity);
    oDeal.setPrice(bdPrice);

    CreateListDeals(oDeal);
}

private void CreateListDeals(deal oDeal){
    itemArray.add(oDeal);
    Collections.sort(itemArray,Collections.reverseOrder());
    itemAdapter.notifyDataSetChanged();
}
}

In my java class I am getting an error with my compareTo method saying:

Type mismatch: cannot convert from int to BigDecimal

I must have missed something along the way, what is it?

Cheers

  • 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-06T20:38:55+00:00Added an answer on June 6, 2026 at 8:38 pm
    public BigDecimal compareTo(deal oDeal) {
        // TODO Auto-generated method stub
        return bdUnitPrice.compareTo(oDeal.getUnitPrice());
    }
    

    Does not match either the signature of the interface, nor does it match the return type of bdUnitPrice.compareTo. Change it to:

    public int compareTo(deal oDeal) {
        // TODO Auto-generated method stub
        return bdUnitPrice.compareTo(oDeal.getUnitPrice());
    }
    

    A couple of other things I should mention (from the comments):

    1. The names of classes should start with an upper case letter. There is a “code conventions” document that spells out that and other things you should do.
    2. You override equals – you should never override equals unless you also override hashCode – otherwise you’ll have tons of problems if you put the objects of this class into a HashSet, HashMap or other collection which uses hash.
    • 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
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

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.