Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
773 views
Welcome To Ask or Share your Answers For Others

1 Answer

i think this is what you are after:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...