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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:27:40+00:00 2026-05-12T14:27:40+00:00

I have a list of objects I need to sort according to properties of

  • 0

I have a list of objects I need to sort according to properties of one of their fields. I’ve heard that SortedMap and Comparators are the best way to do this.

  1. Do I implement Comparable with the class I’m sorting, or do I create a new class?
  2. How do I instantiate the SortedMap and pass in the Comparator?
  3. How does the sorting work? Will it automatically sort everything as new objects are inserted?

EDIT:
This code is giving me an error:

private TreeMap<Ktr> collection = new TreeMap<Ktr>();

(Ktr implements Comparator<Ktr>). Eclipse says it is expecting something like TreeMap<K, V>, so the number of parameters I’m supplying is incorrect.

  • 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-12T14:27:40+00:00Added an answer on May 12, 2026 at 2:27 pm
    1. The simpler way is to implement Comparable with your existing objects, although you could instead create a Comparator and pass it to the SortedMap.
      Note that Comparable and Comparator are two different things; a class implementing Comparable compares this to another object, while a class implementing Comparator compares two other objects.
    2. If you implement Comparable, you don’t need to pass anything special into the constructor. Just call new TreeMap<MyObject>(). (Edit: Except that of course Maps need two generic parameters, not one. Silly me!)
      If you instead create another class implementing Comparator, pass an instance of that class into the constructor.
    3. Yes, according to the TreeMap Javadocs.

    Edit: On re-reading the question, none of this makes sense. If you already have a list, the sensible thing to do is implement Comparable and then call Collections.sort on it. No maps are necessary.

    A little code:

    public class MyObject implements Comparable<MyObject> {
        // ... your existing code here ...
        @Override
        public int compareTo(MyObject other) {
            // do smart things here
        }
    }
    
    // Elsewhere:
    List<MyObject> list = ...;
    Collections.sort(list);
    

    As with the SortedMap, you could instead create a Comparator<MyObject> and pass it to Collections.sort(List, Comparator).

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

Sidebar

Related Questions

I have a list of pointers that reference time objects that need a turn
I have a list of IDs for objects that I need to grab, then
I have two objects List<Report> List<Newsletter> I need certain properties like Id Date Status
I have a stl::list containing Widget class objects. They need to be sorted according
I have a list of objects I need to sort on a field, say
I have an ArrayList of objects that I need to sort in two different
I have a List(Of MyObject) that I need to sort. So I've made sure
I need to sort a list who's items are objects that describe how the
I have an array containing objects that I need to sort, and remove duplicates
I have a list of objects and I need to find an object as

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.