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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:43:11+00:00 2026-05-17T01:43:11+00:00

I am able to compare Strings fine, but would like to know how I

  • 0

I am able to compare Strings fine, but would like to know how I can rank floating point numbers?

getChange() returns a String. I want to be able to sort descending. How can I do this?

UPDATE:

package org.stocktwits.helper;

import java.util.Comparator;

import org.stocktwits.model.Quote;

public class ChangeComparator implements Comparator<Quote>
{
    public int compare(Quote o1, Quote o2) {
        float change1 = Float.valueOf(o1.getChange());
        float change2 = Float.valueOf(o2.getChange());

        if (change1 < change2) return -1;
        if (change1 == change2) return 0; // Fails on NaN however, not sure what you want
        if (change2 > change2) return 1;
    }
}

I am getting the compile time error:

This method must return a result of type int    ChangeComparator.java   
  • 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-17T01:43:12+00:00Added an answer on May 17, 2026 at 1:43 am

    Read the javadoc of Comparator#compare() method.

    Compares its two arguments for order. Returns a negative integer, zero or a positive integer as the first argument is less than, equal to or greater than the second.

    So, basically:

    float change1 = o1.getChange();
    float change2 = o2.getChange();
    if (change1 < change2) return -1;
    if (change1 > change2) return 1;
    return 0;
    

    Or if you like conditional operators:

    return o1.getChange() < o2.getChange() ? -1 
         : o1.getChange() > o2.getChange() ? 1 
         : 0;
    

    You however need to take account with Float.NaN. I am not sure how you’d like to have them ordered. First? Last? Equally?

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

Sidebar

Related Questions

I want to be able to compare an image taken from a webcam to
I want to be able to read line by line from a pdf, compare
Application able to record error in OnError, but we are not able to do
I'm able to connect to and read an excel file no problem. But when
I am changing from MySQL to PostgreSQL but can't find equivalent to MySQL's collation
anyone able to tell me how often a materialized view is set to refresh
You should be able to create a generic form: public partial class MyGenericForm<T> :
I want to be able to capture the exception that is thrown when a
I want to be able to do: For Each thing In things End For
I've been able to find details on several self-balancing BST s through several sources,

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.