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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:49:38+00:00 2026-05-29T08:49:38+00:00

Im trying to sort through an arraylist of objects by a particular value within

  • 0

Im trying to sort through an arraylist of objects by a particular value within the object. What would be the best approach to do such a thing. Should I use Collections.sort() with some kind of comparator?

Im trying to sort a list of objects by a float value they hold in one of the variables.

EDIT:
This is what I have so far:

public class CustomComparator implements Comparator<Marker> {
    @Override
    public int compare(Mark o1, Mark o2) {
        return o1.getDistance().compareTo(o2.getDistance());
    }
}

the error states: Cannot invoke compareTo(double) on the primitive type double.

Is it because a comparator cant return anything other than a certain type?

  • 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-29T08:49:39+00:00Added an answer on May 29, 2026 at 8:49 am

    You should use Comparable instead of a Comparator if a default sort is what your looking for.

    See here, this may be of some help – When should a class be Comparable and/or Comparator?

    Try this –

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class TestSort {
    
        public static void main(String args[]){
    
            ToSort toSort1 = new ToSort(new Float(3), "3");
            ToSort toSort2 = new ToSort(new Float(6), "6");
            ToSort toSort3 = new ToSort(new Float(9), "9");
            ToSort toSort4 = new ToSort(new Float(1), "1");
            ToSort toSort5 = new ToSort(new Float(5), "5");
            ToSort toSort6 = new ToSort(new Float(0), "0");
            ToSort toSort7 = new ToSort(new Float(3), "3");
            ToSort toSort8 = new ToSort(new Float(-3), "-3");
    
            List<ToSort> sortList = new ArrayList<ToSort>();
            sortList.add(toSort1);
            sortList.add(toSort2);
            sortList.add(toSort3);
            sortList.add(toSort4);
            sortList.add(toSort5);
            sortList.add(toSort6);
            sortList.add(toSort7);
            sortList.add(toSort8);
    
            Collections.sort(sortList);
    
            for(ToSort toSort : sortList){
                System.out.println(toSort.toString());
            }
        }
    
    }
    
    public class ToSort implements Comparable<ToSort> {
    
        private Float val;
        private String id;
    
        public ToSort(Float val, String id){
            this.val = val;
            this.id = id;
        }
    
        @Override
        public int compareTo(ToSort f) {
    
            if (val.floatValue() > f.val.floatValue()) {
                return 1;
            }
            else if (val.floatValue() <  f.val.floatValue()) {
                return -1;
            }
            else {
                return 0;
            }
    
        }
    
        @Override
        public String toString(){
            return this.id;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, Trying to sort through the best way to provide access to my Entity
I'm trying to develop a function that can sort through a string that looks
Trying to sort this array of objects according to (1) depth and (2) weight,
I'm trying to sort through a collection of DeepZoom sub-images based on arbitrary data
I'm trying to make a dictionary in Python that I can sort through but
I'm trying to sort through several thousand scanned files and sort them into folders
I am a git novice trying to sort through the concepts and terms. The
I'm trying to sort through a list of 32-bit numbers using MIPS assembler and
I am trying to sort through HTML tags and I can't seem to get
Trying to sort through the advantages or disadvantages of the different approaches. Really not

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.