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

  • Home
  • SEARCH
  • 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 1008805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:49:41+00:00 2026-05-16T08:49:41+00:00

When I write this code, I get an error on the Sort() Method. ArrayList

  • 0

When I write this code, I get an error on the Sort() Method.

       ArrayList al = new ArrayList();
        al.Add("I");
        al.Add("am");
        al.Add(27);
        al.Add("years old");


        foreach (object o in al)
        {
            Console.Write("{0} ", o.ToString());
        }

        al.Sort();
        Console.WriteLine();
        foreach (object o in al)
        {
            Console.Write("{0} ", o.ToString());
        }

Well, I can understand that the Sort Method failed since I included both String and integer in the collection.

But it doesn’t error out when I have all strings or all integers. It does the sorting really well.

  1. What is the property of IComparable implementation, that can possibly error out the mixure?
  2. How does it recognize all integers or all strings for sorting?
  • 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-16T08:49:42+00:00Added an answer on May 16, 2026 at 8:49 am

    It uses the compareTo of the Object. Inside the compareTo, the object will check the type of the compared object and can produce error there like this:

    String : IComparable

    public int compareTo(Object s) {
      if (!(s is String)) {
        throws new Exception();
      }
      //do the job
    }
    

    The sort method of the array will loop through elements of the array and call the compareTo method on each element to compare with other elements

    I also recommend you to use generics so you can never accidentially put different kinds of objects inside. Use ArrayList<String>

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

Sidebar

Related Questions

i write this code in main.java ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); try{ HttpClient httpclient
I write this code to get Dunnet anova post hoc test import rpy2.robjects as
I'm trying to write this code using mostly arrays. I'm trying to get Correct
I've write this code to add textview into table row, but I've problem for
I got error when I write this code @Html.X().ResourceManager() in view error message :
I wrote this code. The constructor works normally, but in the destructor I get
I have write this code but it does work only if I am already
would someone please write this code: this.Loaded += (s, e) => this.loaded = true;
How can I write this code more cleanly/concisely? /// <summary> /// Creates a set
how can i write this code from aspx page in code behind? <a href=skype:MySkype?call><img

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.