0

I have an object called RateQuote that has the following:

  • Name
  • Term
  • Miles

For example:

rate  = new RateQuote() {
  Term = 24,
  Miles = 120000,
  Name = '24 mo. / 120000 miles'
}

and

rate = new RateQuote() {
  Term = 24,
  Miles = 50000,
  Name = '24 mo. / 50000 miles'
} 

and

rate = new RateQuote() {
  Term = 12,
  Miles = 50000,
  Name = '12 mo. / 50000 miles'
} 

These RateQuote's are getting added to a RateQuote[] object:

  public void AddRate(RateQuote rate)
  {
      Rates = Rates.Concat(new RateQuote[] { rate }).ToArray();
  }

After they are added by the .Concat, how would I sort it? If my code adds the "24 mo. / 120000 miles" first, then the "24 mo. / 50,000 miles" second and then the "12 mo. / 50000 miles" last, I would want it to sort it so that it is in this order:

  • 12 mo. / 50000
  • 24 mo. / 50000
  • 24 mo. / 120000

It should sort by months first, then miles. I am confused and would appreciate help. I know there is a .Sort function I can use, but not sure how to use it in this context. Thank you in advance for all of your help.

4
  • Would be much easier to sort before .Concat (using .OrderBy), if possible. And why wouldn't it be? Commented Nov 4, 2013 at 19:06
  • msdn.microsoft.com/en-us/library/… Commented Nov 4, 2013 at 19:07
  • 1
    It's a very strange way to make dynamic array. Why don't you use List<RateQuote>? Commented Nov 4, 2013 at 19:08
  • @Dennis, the code I am working with was written by someone else. It's on my list of things to fix in the future. My company has a lot of things on our plate and changing this to a List<RateQuote> isn't on the top of the list right now. I have it on my list of things to fix when I can. Our code base is large and would take time to change right now. Thanks. Commented Nov 4, 2013 at 19:36

2 Answers 2

9

It appears you want to order first by Term and then by Miles. Then you can use:

var sorted = Rates
               .OrderBy(r=> r.Term)
               .ThenBy(r=> r.Miles)
               .ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

This worked for me. Simple and sorted my list how it should.
@Turp, you are welcome, Also consider using List<RateQuote> instead of an array. You can always get an array back from the list by calling ToArray
I came into my Developer position with an application that already existed. I am slowly, but surely, correcting things like this to make our platform faster and enjoyable for all. This is on my list of things to do, but for now, I just had to sort this list before it was displayed. Due to compliance issues. Thanks!
1

You could use linq, something like:

var orderedRates = rates.GroupBy(rq => rq.Term)
                       .OrderBy(g => g.Key)
                       .SelectMany(g => g.OrderBy(rq => rq.Miles))
                       .ToArray();

11 Comments

this doesn't do what the OP is asking...Sort by Months first then by Miles
@Harrison : please read the question properly and stop negative voting.
@FlyingStreudel, This method works as well. However, to be honest, I am not sure what the difference between yours and Habib's is. Yours just seems like a lot more code. Your thoughts on this?
@Turp I didn't know ThenBy was an extension method :)
This is way complicated code to order an array based on two properties. You are first grouping and then flattening the collection, and that too for no reason. It makes the code hard to read and follow, and should be avoided in production.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.