数组按两个参数排序 [英] Array sorting by two parameters

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

问题描述

我对array.sort有一点困难.我有一个班级,这个班级有两个字段,一个是随机字符串,另一个是随机数.如果我想用一个参数对它进行排序,就可以正常工作.但是我想用两个参数对它进行排序.第一个是数字的总和(从低到高),然后是(如果这些数字与给它们的随机字符串相等)(从低到高).则为THEN.

I'm having a little difficulty with the array.sort. I have a class and this class has two fields, one is a random string the other one is a random number. If i want to sort it with one parameter it just works fine. But i would like to sort it with two parameters. The first one is the SUM of the numbers(from low to high), and THEN if these numbers are equal by the random string that is give to them(from low to high).

您能否给出一些提示和技巧,我如何才能合并"这两种类型?

Can you give some hint and tips how may i can "merge" these two kinds of sort?

Array.Sort(Phonebook, delegate(PBook user1, PBook user2)
        { return user1.Sum().CompareTo(user2.Sum()); });
        Console.WriteLine("ORDER");

        foreach (PBook user in Phonebook)
        {
            Console.WriteLine(user.name);
        }

这就是我使用一个参数对其进行排序的方式.

That's how i order it with one parameter.

推荐答案

我认为这是您追求的目标:

i think this is what you are after:

sourcearray.OrderBy(a=> a.sum).ThenBy(a => a.random)

这篇关于数组按两个参数排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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