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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:03:31+00:00 2026-05-25T19:03:31+00:00

In an interview they told me Write the code in the brackets to order

  • 0

In an interview they told me

Write the code in the brackets to order the list. They said order but you dont know if the type is going to be int or decimal.

They also told me not to use framework methods like .sort

So I have no idea how would I do it? I need to be ready for the next time somebody asks me this.

Possible Inputs: 7,3,8,6,1
Or: 6.9, 4.5, 2.3, 6.1, 9.9

namespace InterViewPreparation1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSort_Click(object sender, EventArgs e)
        {
            List<int> list= new List<int>();
            list.Add(int.Parse(i1.Text));
            list.Add(int.Parse(i2.Text));
            list.Add(int.Parse(i3.Text));
            list.Add(int.Parse(i4.Text));
            list.Add(int.Parse(i5.Text));
            Sort(list);
        }


        private void Sort<T>(List<T> list)
        {
            bool madeChanges;
            int itemCount = list.Count;
            do
            {
                madeChanges = false;
                itemCount--;
                for (int i = 0; i < itemCount; i++)
                {
                    int result = Comparer<T>.Default.Compare(list[i], list[i + 1]);

                    if (result > 0)
                    {
                        Swap(list, i, i + 1);
                        madeChanges = true;
                    }
                }
            } while (madeChanges);            
        }


        public  List<T> Swap<T>(this List<T> list,
                int firstIndex,
                int secondIndex)
        {
            T temp = list[firstIndex];
            list[firstIndex] = list[secondIndex];
            list[secondIndex] = temp;

            return list;
        }
    }
}
  • 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-25T19:03:32+00:00Added an answer on May 25, 2026 at 7:03 pm

    Well, both Int and Double implement IComparable – this means that you should cast each element to an IComparable when performing your sort. As you can’t use any standard .Net sorting method you need to implement one yourself. See Sorting algorithm for some inspiration.

    It would be easier if the method signature was different:

    public void sortlist<T>(List<T> list)
        where T : IComparable
    {
    
    }
    

    An example implementation of bubble sort:

    for (int pass = 1; pass < list.Count; pass++)
    {
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i].CompareTo(list[i + 1]) > 0)
            {
                // Swap
                T temp = list[i];
                list[i] = list[i + 1];
                list[i + 1] = temp;
            }
        }
    }
    

    Alternatively if T isn’t constrained to IComparable then you can tweak this slightly as per Marcs suggestion by using Comparer<T>.Default:

    Comparer<T>.Default.Compare(list[i], list[i + 1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I attended an interview where they asked me to write a C program
I was asked this question during an interview. What they wanted to know was
So I just had a job interview and they gave me a list of
I created a method to organize a generic list without know the type, it
I was at an interview for a C position in which they presented me
I was asked this question during an interview. They're both O(nlogn) and yet most
This was an job placement interview I faced. They asked whether we can realloc
In a past interview, I was asked how would I write a mission critical
One of those classic programming interview questions... You are given two marbles, and told
My boss told me to help in a phone interview with a candidate which

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.