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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:24:45+00:00 2026-05-14T22:24:45+00:00

ok I was going to edit my previous question but i wasnt sure if

  • 0

ok I was going to edit my previous question but i wasnt sure if it was the right way to do it so i’ll just give another question about Comparator, now i want to be able to sort with different ways. I have a bank checks and i want to sort with checkNumber then checkAmount
i managed to do it with checkNumber but couldnt figure out how with checkAmount
here is how i did it for checkNumber:

import java.util.Comparator;

public class Check implements Comparator {
    private int checkNumber;
    private String description;
    private double checkAmount;

    public Check() {


    }


    public Check(int newCheckNumber, double newAmountNumber) {

        setCheckNumber(newCheckNumber);
        setAmountNumber(newAmountNumber);
    }

    public String toString() {
        return  checkNumber + "\t\t" + checkAmount;
    }

    public void setCheckNumber(int checkNumber) {
        this.checkNumber = checkNumber;
    }

    public int getCheckNumber() {
        return checkNumber;
    }

    public void setAmountNumber(double amountNumber) {
        this.checkAmount = amountNumber;
    }

    public double getAmountNumber() {
        return checkAmount;
    }

    @Override
    public int compare(Object obj1, Object obj2) {

         int value1 = ((Check) obj1).getCheckNumber();
        int value2 = ((Check) obj2).getCheckNumber();

         int result = 0;

         if (value1 > value2){
             result = 1;
         }
         else if(value1 < value2){
             result = -1;
         }
         return result;
    }
}



import java.util.ArrayList;
import java.util.Collections;

import test.CheckValue;

public class TestCheck {

    public static void main(String[] args) {
        ArrayList List = new ArrayList();


        List.add(new Check(445, 55.0));
        List.add(new Check(101,43.12));
        List.add(new Check(110,101.0));
        List.add(new Check(553,300.21));
        List.add(new Check(123,32.1));


        Collections.sort(List, new Check());

        System.out.println("Check Number - Check Amount");

        for (int i = 0; i < List.size(); i++){

            System.out.println(List.get(i));
        }



    }

}

thank you very much in advance and please tell me if im submiting things in the wrong way.

  • 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-14T22:24:46+00:00Added an answer on May 14, 2026 at 10:24 pm

    What you really want to do is define a separate class to act as the Comparator object – don’t make your actual Check class the comparator, but instead have 3 classes:

    1. the Check class itself
    2. a CheckAmountComparator class (or something similar) that implements Comparator<Check>
    3. a CheckNumberComparator class (or something similar) that implements Comparator<Check>

    Then when you want to sort one way or another, you simply pass an instance of the Comparator-implementing class corresponding to the type of sorting you want to do. For instance, to sort by amount, it’d then become…

    Collections.sort(yourListVariable, new CheckAmountComparator());
    

    Also – I’d highly suggest naming your variable something other than List, since List is used as a type name in Java.

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

Sidebar

Related Questions

I'm going to edit textbox value.. but i saw there's a problem protected void
Edit: OK I asked the wrong question here. I'm going to be coding a
Edit: It turns out I missed something obvious, but I'm going to leave the
[Edit] I have reedited the previous question - since I had misunderstood what was
I want to visible a edit button on my previous activity by going back
This is a continuation question from a previous question I have asked I now
I'm going to edit an ImageField using jquery ajax,after searching I found out I
In PHP I am going to edit some entity let say a project named:
Going to http://www.example.com/node/NID/edit , where NID is any valid nid referring to a node
I'm going to try building a custom language extension for Komodo Edit. In order

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.