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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:06:03+00:00 2026-06-03T04:06:03+00:00

I am attempting to sort an ArrayList of TokenDoubleCounters (my custom object). In TokenDoubleCounter,

  • 0

I am attempting to sort an ArrayList of TokenDoubleCounters (my custom object). In TokenDoubleCounter, I have implemented equals and compareTo as below:

public class TokenDoubleCounter implements Comparable<TokenDoubleCounter> {
private String word;
private double count;

public boolean equals(Object o) {
    if (o instanceof TokenDoubleCounter) {
        TokenDoubleCounter other = (TokenDoubleCounter) o;
        if (other.word.equals(this.word) && other.count == this.count)
            return true;
    }
    return false;
}

public int compareTo(TokenDoubleCounter other) {
    double result = this.count - other.getCount();
    if (result > 0.0) 
        return 1;
    if (result < 0.0) 
        return -1;
    return this.word.compareTo(other.getWord());
}

    //rest of class omitted
}

These objects are created and sorted in the following function call:

    public List<TokenDoubleCounter> chiSquareValueAll(int cl, int cl2) {
    List<TokenDoubleCounter> list = new ArrayList<TokenDoubleCounter>();

    for (String word : map.keySet()) {
                                                        //chiSquareValue2 returns a double
        list.add(new TokenDoubleCounter(word, chiSquareValue2(cl,cl2,word)));       
    }
    Collections.sort(list, Collections.reverseOrder());
    return list;
}

Finally, iterating through these results, I write these to file:

    public boolean printChiSquare(PrintWriter out, int cl, int cl2) {
    for (TokenDoubleCounter tdc : this.chiSquareValueAll(cl,cl2)) {
        if (tdc.getCount() > 2.7) {
             //getWord() returns string value "word"  and getCount() returns double value "count"
            out.write(tdc.getWord() + "," + tdc.getCount() + "\n");

        }
    }
    return true;
}

The results were somewhat surprising to me as they do not seem to be in the order I requested:

word,8.937254901960785
word,8.937254901960785
word,8.937254901960785
word,5.460792811839323
word,4.746170542635659
word,4.382692307692308
word,4.382692307692308
word,4.382692307692308
word,4.382692307692308
word,4.382692307692308
word,4.382692307692308
word,4.382692307692308
word,8.937254901960785
word,8.937254901960785
word,8.937254901960785
word,8.937254901960785
word,8.937254901960785
word,8.937254901960785

word,8.937254901960785
word,5.460792811839323
word,4.746170542635659
word,4.746170542635659
word,4.746170542635659
word,4.382692307692308

…

What am I missing? And let me know if you need additional details.

Also, I should add that all of the entries “word” are actually strings of varying length, etc., but I don’t think that’s relevant.

Thanks for the help.

  • 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-03T04:06:04+00:00Added an answer on June 3, 2026 at 4:06 am

    Try this:

    public int compareTo(TokenDoubleCounter other) {
        int result = Double.compare(count, other.count);
        return result != 0 ? result : word.compareTo(other.word);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have sort of a tricky problem I'm attempting to solve. First of all,
I am attempting sort this array by each stdClass Object's [title] in an Ubercart
I'm attempting to sort of highlight a tile object within a game I'm making
I'm attempting to SORT an ArrayList by (Type Date) MyObject.getDate() My format is formatted
I'm attempting to do a simple bubble sort code to get familiar with list/string
Attempting/struggling to get registration and sign-up working within an active admin project. I have
Attempting a beginner's tutorial. I have the following in my head: <script type=text/javascript charset=utf-8
I'm attempting to sort a ListView using C#, but whenever I click the sort
So I am pulling a list of links and I am attempting to sort
I have implemented a Section Index for my tableview. It returns the correct letters

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.