Link to home
Start Free TrialLog in
Avatar of mikha
mikhaFlag for United States of America

asked on

linq sorting, converting back to list

I have working with an Rest api, which returns json data, but not sorted out the way I want. Api is not flexible enough, such that we can pass a sort parameter and it returns the result accordingly.

so, the data in such form

public class records {

....
      public Collection<Item> items ;

.....

}


in my c# code, I do something like this,


var results = CallApi();


var sortedResults = results.OrderBy(x => x.items.price);

return soretedResults.tolist();


I am concerned that this sorting and converting it back to the list , is expensive and as the results
grow larger, this might cause problem.

any thoughts in this?
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Expensive is relative. If your application runs from start to finish in 1 second, then every millisecond can be important. If your application takes minutes to run and this sorting only takes milliseconds, then it might not be a big deal.

Generally speaking, that's about as fast as you can run it without resorting to custom code that might be harder to maintain.
ASKER CERTIFIED SOLUTION
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, I should note that it's not -very- expensive. You're not recreating the objects themselves - you're simply building a container, and that usually happens pretty quickly. You can use a performance profiler (e.g. ANTS) to evaluate how much time it takes, and if you're concerned about it impacting the UI or user experience, you can move that to a separate non-UI thread (e.g. using async Task). Just be aware that async does have a bit of overhead to set up, and there's a chance that it might be more overhead than the sorting/conversion process.
Worry about performance when it's time to worry about performance. So what if the sort takes 600 ms instead of 300 ms? Get something that works first, then, if performance is indeed an issue, profile your code and track down the hotspots for performance. Don't pre-optimize your code for something you're not certain is going to be an issue.
Avatar of Bill Prew
Bill Prew

I agree with the advice you are getting, worry about the sorting performance only if it becomes an issue.  I would expect that it would only be a factor if you were dealing with very large numbers of items in the list, and as mentioned in that case the delays of the API call and getting the data back are probably going to be much larger than the sort.

You could look at SortedList, or Dictionary classes, but I expect that they may actually be a bit slower since they do the "sorting" on insert rather than one time after all items are in the list.  But at the end of the day I would expect that sorting the list, versus maintaining a sorted key "tree" are going to take about the same amount of time.  The simple list approach may use more memory to store it in, but the sort process could use more memory to process.  While the SortedList or Dictionary approaches likely require more memory to store the structure(s).

So there are different ways to approach this but I think the simple straight forward approach you are using makes the most sense, and you stick with it until it becomes a problem, and then make sure you debug that properly and apply the pareto principle to focus on the most critical time sensitive areas of the code to optimize.


»bp
@mikha,

Are you all set with this now, or do you need more help?  If all set, could you please close it out now.  If you need help with the question close process take a look at:



»bp