简体   繁体   English

关于其子元素在c#中对arraylist进行排序

[英]Sorting an arraylist in c# regarding to its sub-elements

I have an ArrayList which its elements are an Array . 我有一个ArrayList ,它的元素是Array What I want to do is to sort this ArrayList using elements of Array . 我想要做的是使用Array元素对此ArrayList进行排序。 To clarify, let's suppose we have the following arraylist: 为澄清起见,让我们假设我们有以下arraylist:

arraylist{[10,2], [6,3], [12,2]}

Then I want to sort it in descending order using dividing each elements of arrays to each other(array a_i = [a_i,b_i] ==> sort using max(a_i/b_i) ), ie, in the following form: 然后,我想通过使用数组的每个元素彼此划分(数组a_i = [a_i,b_i] ==> sort using max(a_i/b_i)进行排序)以降序对它进行排序,即采用以下形式:

arraylist{[12,2],[10,2],[6,3]}

I tried using Sort() , but it is for a list not for an ArrayList as I know. 我尝试使用Sort() ,但是据我所知,它用于列表而不是ArrayList Would anyone please let me know how I could sort it in an efficient way to avoid a high complexity? 有人能让我知道如何有效地对它进行排序以避免高复杂度吗?

Thanks! 谢谢!

ArrayList is deprecated and you really should not use it anymore..: ArrayList已过时,您真的不应该再使用它了..:

MSDN : We don't recommend that you use the ArrayList class for new development. MSDN :我们不建议您将ArrayList类用于新开发。 Instead, we recommend that you use the generic List class. 相反,我们建议您使用通用List类。

Let's assume you have created a List<Tuple<int, int>> maybe like this: 假设您已经创建了一个List<Tuple<int, int>>也许像这样:

var tupleList = new List<Tuple<int, int>>
{
    new Tuple<int, int>(10,2 ),
    new Tuple<int, int>(6,3),
    new Tuple<int, int>(12,2)
};

Then the result can be created with Linq, maybe like this: 然后可以使用Linq创建结果,也许像这样:

var sorted = tupleList.OrderByDescending(x => x.Item1 / x.Item2);

This omits any checks for div by zero.. 这会忽略对div的任何检查(零)。

To make the initialization a bit shorter you can use a function.. 要使初始化短一些,可以使用一个函数。

Func<int, int, Tuple<int, int>> tc = Tuple.Create;

..taken from here and write: ..从这里获取并写道:

var tupleList = new List<Tuple<int, int>>
{
    tc(10,2 ),
    tc(6,3 ),
    tc(12,2 )
};

Like someone said in the comments, you're better off using List<T> . 就像评论中有人说的那样,最好使用List<T> But here's how to do it using Sort() with an IComparer() 但是这是将Sort()与IComparer()结合使用的方法

class DivisionComparer : IComparer<int[]>, IComparer
{

    public int Compare(int[] x, int[] y)
    {
        double xval = (double) x[0] / x[1];
        double yval = (double) y[0] / y[1];
        return yval.CompareTo(xval);
    }

    public int Compare(object x, object y)
    {
        return Compare((int[]) x, (int[]) y);
    }
}


class Program
{

    static void Main(string[] args)
    {

        var x = new ArrayList()
        {
            new [] {6,3},                
            new [] {12,2},
            new [] {10,2},

        };
        x.Sort(new DivisionComparer());
        foreach (var el in x)
        {
            Debug.WriteLine(string.Join(",", (int[]) el));
        }
    }
}

This will output: 这将输出:

12,2
10,2
6,3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM