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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:32:42+00:00 2026-05-28T07:32:42+00:00

I am looking for a data structure that will sort objects based on a

  • 0

I am looking for a data structure that will sort objects based on a given integer. Like so:

Object    Score
Object1   86
Object2   85
Object3   85 

Each object will be unique, but there can be duplicate scores. Was trying to devise a way on how a Map object could do this, but can’t figure it out.

Structure can either sort the objects during/after an insertion or when I implicitly call for a sort.

Is there a structure out there that does this already? Or should I code it myself?

  • 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-28T07:32:43+00:00Added an answer on May 28, 2026 at 7:32 am

    Try putting the objects in a SortedSet if you want unique values or a List if you want to see duplicates, along with a custom Comparator to sort them based on their “scores”.

    Providing your own Comparator will let you specify that the objects should be sorted on their score field and how.

    The Comparator is also reusable and easy to swap in / out if you wanted to, say do a descending sort on the items.

    So an example that might be:

      class ScoreComparator implements Comparator <ObjectHolder> {
        @Override
        public int compare(ObjectHolder o1, ObjectHolder o2) {
          return o1.getScore().compareTo(o2.getScore());
        }
      }
    
      class DescendingScoreComparator implements Comparator <ObjectHolder> {
        @Override
        public int compare(ObjectHolder o1, ObjectHolder o2) {
          return o2.getScore().compareTo(o1.getScore());
        }
      }
    
    
      class ObjectHolder {
        Object obj;
        Integer score;
    
        public ObjectHolder(Object o, Integer score) {
          this.obj = o;
          this.score = score;
        }
    
        public Object getObject() {
          return obj;
        }
        public Integer getScore() {
          return score;
        }
      }
    
    
      public void showExample() {
        SortedSet<ObjectHolder> sortedSet = new TreeSet<ObjectHolder>(new ScoreComparator());
        sortedSet.add(new ObjectHolder("addedFirst", 55));
        sortedSet.add(new ObjectHolder("addedSecond", 25));
        sortedSet.add(new ObjectHolder("addedThird", 75));
        sortedSet.add(new ObjectHolder("addedFourth", 25));
        sortedSet.add(new ObjectHolder("addedFifth", 95));
    
        // The resulting set will only have 4 items since sets don't allow duplicates
        for (ObjectHolder holder : sortedSet) {
          System.out.println(holder.getScore());
        }
    
        List<ObjectHolder> list = new LinkedList<ObjectHolder>();
        list.add(new ObjectHolder("addedFirst", 55));
        list.add(new ObjectHolder("addedSecond", 25));
        list.add(new ObjectHolder("addedThird", 75));
        list.add(new ObjectHolder("addedFourth", 25));
        list.add(new ObjectHolder("addedFifth", 95));
    
        Collections.sort(list, new ScoreComparator());
    
        // The resulting set will only have 5 items since lists allow duplicates
        System.out.println();
        for (ObjectHolder holder : list) {
          System.out.println(holder.getScore());
        }
    
        Collections.sort(list, new DescendingScoreComparator());
    
        System.out.println("\nWill print 5 items, but this time in descending order");
        for (ObjectHolder holder : list) {
          System.out.println(holder.getScore());
        }
      }
    

    And here’s the output of the above code:

    Will print 4 unique items since sets don’t allow duplicates

    25 55 75 95

    Will print 5 items since lists do allow duplicates

    25 25 55 75 95

    Will print 5 items, but this time in descending order

    95 75 55 25 25

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

Sidebar

Related Questions

I'm looking for a data structure that will act like a Queue so that
I'm looking for a sorted data structure, that will be similar to the STL
I am looking for a data structure that orders objects at insertion efficiently. I
I'm looking for a data structure that will store any DAG, but can efficiently
I am looking for a data structure that operates similar to a hash table,
I'm looking for a data structure that provides indexing for Rectangles. I need the
I'm looking for a Java built-in data structure that would be the best at
I'm looking for a data structure (or structures) that would allow me keep me
I'm looking for a tool that finds duplicate nodes in a tree data structure
I'm looking for a data structure that has the following properties. Stores a list

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.