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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:57:41+00:00 2026-05-22T19:57:41+00:00

I created a my own SortedSet here is the code for adding something to

  • 0

I created a my own SortedSet here is the code for adding something to the array. (I know there are better and simpler ways to do this than an Array but it has to be done this way)

public boolean add(AnyType x){
    if(this.contains(x))
        return false;
    else if(this.isEmpty()){
        items[theSize]=x;
        theSize++;
        return true;
    }
    else{
    if( theSize == items.length )
        this.grow();
    //Here goes code for adding
    theSize++;
    AnyType[] newItems = (AnyType[]) new Comparable[theSize];
    for(int i=0;i<theSize-1;i++)
        if(items[i].compareTo(x)>0){
            newItems[i]=x;
            newItems[i+1]=items[i];
            for(int j=i+2;j<theSize;j++)
                newItems[j]=items[j-1];
            items = newItems;
            return true;
        }
        else
            newItems[i]=items[i];
    newItems[theSize-1] =x;
    items=newItems;
    return true; //*/
    }
}

And I am testing sorted number strings like this:

public static void main(String[] a) {


    SortedSet<String> s1 = new SortedSet<String>();
    final int NUM1 = 37;
    final int NUM2 = 53;
    final int NUM3 = 61;

    for (int i = NUM1; i != 0; i = (i + NUM1) % NUM3) {
        s1.add("" + i);
    }
    s1.show();
    for (int i = NUM1; i != 0; i = (i + NUM1) % NUM3) {
        s1.add("" + i);
    }
    s1.show();
    for (int i = NUM1; i != 0; i = (i + NUM1) % NUM2) {
        s1.remove("" + i);
    }

    s1.show();

    }

And in the output I get:

1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30 31 32 33 34 35 36 37 38 39 4 40 41 42 43 44 45 46 47 48 49 5 50 51 52 53 54 55 56 57 58 59 6 60 7 8 9

My question is how do I make this to be sorted the way it should be?? I know the problem is in the adding method (it should be able to sort strings and integers as well)

And it works perfectly fine when I create a SortedSet of Strings or Integers, when when I mix them like this I get this “unsorted” outcome.

Help??
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-22T19:57:41+00:00Added an answer on May 22, 2026 at 7:57 pm

    Those look like sorted strings to me. “1” comes before “10” just like “a” comes before “ab” in the dictionary. @MRAB has the correct suggestion to convert your strings representing numbers to actual numbers if you want them sorted in numerical order.

    You can do that with a Comparator if you want to keep your set a SortedSet<String> (error checking not performed in the snippet below):

        SortedSet<String> s1 = new TreeSet<String>(new Comparator<String>() {
            /**
             * Returns a positive value if number1 is larger than number 2, a
             * negative number if number1 is less than number2, and 0 if they
             * are equal.
             */
            public int compare(String number1, String number2) {
                return Integer.parseInt(number1) - Integer.parseInt(number2);
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created my own ProfileUIViewController class that is a UINavigationControllerDelegate . I display this
I created my own CMS for my own use. currently, I already launch 3
I created my own zoom bar for a Google map, but it's not working
I created my own parental control app using C# to monitor my kids activity.
I created my own class for PopoverController (Without subclassing UIPopoverController) to present ViewControllers in
I created my own entity to go with the stored procedure I wrote. The
I have successfully created my own virtual path provider to load a user control
I've created my own extending function, but I´m not sure if it is good
I have created my own simple logger class which uses COM interop to redirect
I have been creating an QT application but struck in a place. I have

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.