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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:27:23+00:00 2026-06-07T15:27:23+00:00

Im trying to implement a comparator for a custom class so I can choose

  • 0

Im trying to implement a comparator for a custom class so I can choose which field to sort by. one of the comparator classes I made is giving me an error I do not understand in the slightest. feel free to point out anything you see, beacause my boss left early on friday and I didn’t get to ask him questions once I started trying to sort an array of the “Spectrum” objects in the code below:

import java.io.*;
import java.util.*;

public class scanComparator<Spectrum> implements Comparator<Spectrum> {
    public int compare(Spectrum o1, Spectrum o2){
        int s1 = o1.getScanNumber();
        int s2 = o2.getScanNumber();
        int eval = s2 - s1;
        return eval;
    }
}

this is the error (from jGrasp):

scanComparator.java:7: error: cannot find symbol
        int s2 = o2.getScanNumber();
                   ^
  symbol:   method getScanNumber()
  location: variable o2 of type Spectrum
  where Spectrum is a type-variable:
    Spectrum extends Object declared in class scanComparator

getScanNumber() is public and not miss-spelled. I re copy-pasted the method name from the Spectrum class. the same error is given for o1 but ommitted because its redundant.

  • 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-07T15:27:25+00:00Added an answer on June 7, 2026 at 3:27 pm

    Your problem is you are declaring scanComparator<Spectrum>, just change it to ‘scanComparator’ as follow and everything goes fine.

    import java.io.*;
    import java.util.*;
    
    public class scanComparator implements Comparator<Spectrum> {
        public int compare(Spectrum o1, Spectrum o2){
            int s1 = o1.getScanNumber();
            int s2 = o2.getScanNumber();
            int eval = s2 - s1;
            return eval;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement compareTo on a domain class in grails so I can
I'm trying to sort an ArrayList of custom items and get 'At least one
I am trying to create a class implementing Blackberry's Comparator so I can easily
I have an abstract class which defines a number of classes which can be
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
I'm trying to implement a search method, which returns a index of a object
I am trying to implement a sorted list. I have created the class I
I'm trying to write a test for the following method: public class Sort {
Every time I need to implement a comparator, I get stuck trying to remember
I am a beginner with trees and I was just trying to implement one

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.