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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:28:38+00:00 2026-05-19T09:28:38+00:00

namespace SortableLists { using System; using System.Collections.Generic; public class Program { private static void

  • 0
namespace SortableLists
{
    using System;
    using System.Collections.Generic;

    public class Program
    {
        private static void Main() {
            var list = new List<ListItem>
                           {
                               new ListItem {AdmissionCode = "801r", Name = "Rajesh Koothrappali", RollNumber = 54},
                               new ListItem {AdmissionCode = "892k", Name = "Leonard Leakey Hofstadter", RollNumber = 34},
                               new ListItem {AdmissionCode = "1203a", Name = "Sheldon Lee Cooper", RollNumber = 46},
                               new ListItem {AdmissionCode = "802x", Name = "Howard Wolowitz", RollNumber = 98}
                           };
            list.ForEach(x => Console.WriteLine(x.RollNumber + ","+x.Name + "," + x.AdmissionCode));

            Console.Write("\n");
            list.Sort();
            list.ForEach(x => Console.WriteLine(x.RollNumber + "," + x.Name + "," + x.AdmissionCode));

            Console.ReadKey();
        }
    }

    public class ListItem : IComparable<ListItem>
    {
        public int RollNumber { get; set; }
        public string Name { get; set; }
        public string AdmissionCode { get; set; }

        #region Implementation of IComparable<in ListItem>

        public int CompareTo(ListItem other) {
            return AdmissionCode.CompareTo(other.AdmissionCode);
        }

        #endregion
    }
}

I dont know what kind of sorting this is where Admission code 1203 Dr. Sheldon shows up at the top of the list after sorting??? i was expecting 801,802,803 and 1203… can anyone explain?

  • 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-19T09:28:39+00:00Added an answer on May 19, 2026 at 9:28 am

    The numbers you are comparing are not treated as numbers but as strings! And with strings the letter ‘1’ comes before ‘8’, so the larger number appears first because when treated as text, the order is different.

    I recommend you convert this field to an int if you wish to treat it as one.

    Edit: for your edited question (field now also contains letters), you will need to write custom comparison logic to compare them in the order you desire.

    For example, I imagine you want the logic to be like this:

    1. Split code into number & letters.
    2. Compare numbers only (as int).
    3. If number parts are the same for both values, compare the rest as string.

    Implement this logic (or whatever logic you really want) in your CompareTo method and you’ll have your desired order.

    The code for such logic might be like this:

    public class ListItem : IComparable<ListItem>
    {
        public int RollNumber { get; set; }
        public string Name { get; set; }
        public string AdmissionCode { get; set; }
    
        private static readonly char[] Numbers = new[]
        {
            '0',
            '1',
            '2',
            '3',
            '4',
            '5',
            '6',
            '7',
            '8',
            '9'
        };
    
        #region Implementation of IComparable<in ListItem>
        public int CompareTo(ListItem other)
        {
            // Assumes AdmissionCode is in ####ABC format,
            // with at least one number and any amount of letters.
            string myNumberPart, myRemainingPart;
            string otherNumberPart, otherRemainingPart;
    
            SplitAdmissionCode(AdmissionCode, out myNumberPart, out myRemainingPart);
            SplitAdmissionCode(other.AdmissionCode, out otherNumberPart, out otherRemainingPart);
    
            int myNumber = int.Parse(myNumberPart);
            int otherNumber = int.Parse(otherNumberPart);
    
            int result = myNumber.CompareTo(otherNumber);
    
            // Numbers are different.
            if (result != 0)
                return result;
    
            // Numbers are same. Use text compare for the remaining part.
            return myRemainingPart.CompareTo(otherRemainingPart);
        }
    
        private void SplitAdmissionCode(string code, out string numbersPart, out string remainingPart)
        {
            int lastNumberIndex = code.LastIndexOfAny(Numbers);
    
            numbersPart = code.Substring(0, lastNumberIndex + 1);
    
            if (lastNumberIndex == code.Length - 1)
                remainingPart = "";
            else
                remainingPart = code.Substring(lastNumberIndex + 1);
        }
        #endregion
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I've got the following program: namespace ReflectionTest { public class Example { private
in the System.Linq namespace, we can now extend our IEnumerable's to have the Any()
I place using namespace in a view code behind but i can't call any
The .NET System.Security.Cryptography namespace has a rather bewildering collection of algorithms that I could
My colleague insists on explicitly specifying the namespace in code as opposed to using
Namespace are pretty cool: with them, you can organize your libraries and you can
My schema specifies a namespace, but the documents don't. What's the simplest way to
In Microsoft's UnitTesting namespace ( Microsoft.VisualStudio.TestTools.UnitTesting ) there are AssemblyInitialize and AssemblyCleanup attributes you
I have a small namespace containing some type definitions, which I use to make
When authoring a library in a particular namespace, it's often convenient to provide overloaded

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.