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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:57:55+00:00 2026-05-13T14:57:55+00:00

I am building a class that has a mapping of strings to integers. So

  • 0

I am building a class that has a mapping of strings to integers. So if I have 3 apples I would have a mapping of apples to 3.

I need to write a class that sorts the name of the objects by decreasing numbers.

So if I have

(apples, 3)
(oranges, 2)
(bananas, 5)

I will get
(bananas, 5), (apples, 3), (oranges 2)

I was wondering if there’s already a class out there that would make my life easier or how I would implement this.

Thanks.

  • 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-13T14:57:55+00:00Added an answer on May 13, 2026 at 2:57 pm

    You should be able to put your objects (apples, 3) (oranges, 2) (bananas, 5) into a List and then call Collections.sort(yourlist). You’d then want to make sure the object you declared implements the Comparable interface.

    More information is available at http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

    Let’s say you declared you object as

    public class FruitAndCount implements Comparable<FruitAndCount> {
        private final String name;
        private final Integer count;
    
        public FruitAndCount(String name, int count) {
            this.name = name;
            this.count = count;
        }
    
        public String name() { return name;  }
        public int count()   { return count; }
    
        public int compareTo(FruitAndCount o) {
            return this.count.compareTo(o.count);
        }
    }
    

    You should then be able to make the following call which will sort your list:

    FruitAndCount fruitArray[] = {
        new FruitAndCount("Apples", 3),
        new FruitAndCount("Oranges", 2),
        new FruitAndCount("Bananas", 5)
    };
    
    List<FruitAndCount> fruit = Arrays.asList(fruitArray);
    Collections.sort(fruit);
    

    You should then have a sorted list of fruit.

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

Sidebar

Related Questions

I am building a tool. I have a Collection Class that stores data gathered
I'm building a class that interacts with an API. Say the API has a
Say I'm building a UDF class called StaticLookupUDF that has to load some static
I'm building a relatively simple class (ex: Person) for an ASP.NET application that has
I'm building a GAE app that has objects with a lot of boolean properties.
I am building a FLA file that has a document class Main and in
I have a JSF Validator that I'm building that has properties in it that
I have a web app I'm building that has about 50 forms. I have
I am building a class that has a union for its matrix data, however,
To clarify: I'm building a Logger class that allows me to easily log messages:

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.