泛型问题排序 [英] Generics Question ordering

查看:82
本文介绍了泛型问题排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们在接受采访时告诉我

In an interview they told me

在方括号中编写代码以排序列表.他们说顺序,但是您不知道类型是int还是decimal.

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.

他们还告诉我不要使用.sort

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.

可能的输入:7、3、8、6、1
或:6.9、4.5、2.3、6.1、9.9

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;
        }
    }
}

推荐答案

好吧,这两个 Double 实现 IComparable -这意味着您在执行时应将每个元素都转换为IComparable您的排序.由于您无法使用任何标准的.Net排序方法,因此您需要自己实现.请参阅排序算法.

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
{

}

气泡排序的示例实现:

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;
        }
    }
}

或者,如果T不限于IComparable,则可以使用Comparer<T>.Default根据Marcs的建议进行微调:

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])

这篇关于泛型问题排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆