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 would like to be able to compare two classes derived from the same
How can I get the previous focus/be able to compare strings?
I want to be able to compare one string to all other strings sequentially
Would anyone please be able to tell me the most efficient way to compare
Some programs like ProcessExplorer are able to read strings in memory (for example, my
Some programs like ProcessExplorer are able to read strings in memory (for example, my
I want to convert two strings to ints to be able to compare them.
Question, I'd like to be able to compare a list of objects against an
I am creating a music website where I would like users to be able
Using NUnit and NMock2 I was not able to compare what I thought were

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.