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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:44:18+00:00 2026-05-14T07:44:18+00:00

Here’s the deal : I have Publication objets in my application. I also have

  • 0

Here’s the deal :

I have Publication objets in my application.
I also have Vote objet (forOrAgainst,author,linkedPublication)

I want to sort publication by date, title… and also by number of votes.

I cant directly sort my list of publication as i dont have the number of vote in that list.
How can i sort my list of publication without adding method to the publication object.

What is the best way to link them ?

Should i return a hashmap ? a treeset ? an array ?

It’s kinda messy in my brain now…

  • 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-14T07:44:19+00:00Added an answer on May 14, 2026 at 7:44 am

    Here’s an example of using Comparator to sort based on an external criteria:

    import java.util.*;
    
    class VoteComparator implements Comparator<String> {
        final Map<String, Integer> tally;
        VoteComparator(Map<String, Integer> tally) {
            this.tally = tally;
        }
        @Override public int compare(String pub1, String pub2) {
            int v1 = tally.get(pub1);
            int v2 = tally.get(pub2);
            return
               (v1 < v2) ? -1 :
               (v1 > v2) ? +1 :
               0;
        }           
    };
    

    This uses just String for publication for simplicity; you’d want to sort Publication in your application. This also uses a simple int to get the vote count, but essentially there has to be a tally service that gives you, given a Publication, what its Vote count is.

    Note: English is not my first language, so perhaps “tally” isn’t the right word for it, but basically some sort of vote registrar, vote recorder, essentially a map between an object, and how many votes it gets.

    Then you can sort, using, say, TreeSet.

    public class SortExample {
        public static void main(String[] args) {
            Map<String, Integer> tally = new HashMap<String, Integer>();
            tally.put("foo", 42);
            tally.put("bar", 13);
            tally.put("Fizz", 3);
            tally.put("Buzz", 5);
            tally.put("FizzBuzz", 15);
    
            Comparator<String> voteComparator = new VoteComparator(tally);
            SortedSet<String> sortedByVote = new TreeSet<String>(voteComparator);
            sortedByVote.addAll(tally.keySet());
            for (String pub: sortedByVote) {
                System.out.println(pub + " (" + tally.get(pub) + " votes)");
            }
        }
    }
    

    This prints:

    Fizz (3 votes)
    Buzz (5 votes)
    bar (13 votes)
    FizzBuzz (15 votes)
    foo (42 votes)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here is an example: I have the generic type called Account. I wish to
Here is the problem that I am trying to solve. I have two folders
Here's what I'm doing. I want to upload multipart file via Ajax to my
Here is my scenario. I have a website running under AppPool1 and that works
Here is my problem.. I have the option to create(add) two new input fields,
Here are the tables I have: Table A which has entries with item and
Here's the code I have. It works. The only problem is that the first

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.