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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:29:52+00:00 2026-06-13T02:29:52+00:00

I want to implement Comparable for one of my class. My test-Array: A1, A2,

  • 0

I want to implement Comparable for one of my class.

My test-Array: A1, A2, A3, B1, B2, B3

I want so sort like this:
-sorting by letter

-if A then ascending number

-if B then descending number

Outcome expected: A1, A2, A3, B3, B2, B1

The following code would always give me letter and numbers ascending, like A1, A2, A3, B1, B2, B3.
How can I switch the order of numbers based on which letter Foo has?

class Foo implements Comparable {
    String letter, int number;

    int compareTo(Foo f) {
        int x = letter.compareTo(f.letter);
        return x != 0 ? x : number.compareTo(f.number);
    }
}
  • 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-06-13T02:29:54+00:00Added an answer on June 13, 2026 at 2:29 am

    You could do something like this:

    int compareTo(Foo f) {
        int x = letter.compareTo(f.letter);
        return x != 0 ? x 
             : letter.equals("A") ? number.compareTo(f.number)
                                  : f.number.compareTo(number);
    }
    

    (That’s assuming you want to treat all non-A values as descending, of course…)

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

Sidebar

Related Questions

given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I have a list of objects which implement Comparable . I want to sort
I have a class which implements comparable to compare and sort an array of
i starter in jqgrid, i want implement inline edit in jqgrid i have this
I want to keep two things in my priority queue...one is a number and
I want to implement an ajax Client that does polling to the server. This
I want to sort a WPF-Toolkit DataGrid on a column which is bound like
As titled. We all know if we want a class to be comparable and
I want implement in my software solution an VBA editor but in c# 3.0.
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it

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.