Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 628509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:38:40+00:00 2026-05-13T19:38:40+00:00

I have the following method to apply a sort to a list of objects

  • 0

I have the following method to apply a sort to a list of objects (simplified it for the example):

private IEnumerable<Something> SetupOrderSort(IEnumerable<Something> input,
    SORT_TYPE sort)
{
    IOrderedEnumerable<Something> output = input.OrderBy(s => s.FieldA).
        ThenBy(s => s.FieldB);
    switch (sort)
    {
        case SORT_TYPE.FIELD1:
            output = output.ThenBy(s => s.Field1);
            break;
        case SORT_TYPE.FIELD2:
            output = output.ThenBy(s => s.Field2);
            break;
        case SORT_TYPE.UNDEFINED:
            break;
    }
    return output.ThenBy(s => s.FieldC).ThenBy(s => s.FieldD).
        AsEnumerable();
}

What I needs is to be able to insert a specific field in the midst of the orby clause. By default the ordering is: FIELDA, FIELDB, FIELDC, FIELDD.

When a sort field is specified though I need to insert the specified field between FIELDB and FIELDC in the sort order.

Currently there is only 2 possible fields to sort by but could be up to 8. Performance wise is this a good approach? Is there a more efficient way of doing this?

EDIT: I saw the following thread as well: Dynamic LINQ OrderBy on IEnumerable<T> but I thought it was overkill for what I needed. This is a snippet of code that executes a lot so I just want to make sure I am not doing something that could be easily done better that I am just missing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:38:41+00:00Added an answer on May 13, 2026 at 7:38 pm

    There’s nothing inefficient about your method, but there is something unintuitive about it, which is that you can’t sort by multiple columns – something that end users are almost sure to want to do.

    I might hand-wave this concern away on the chance that both columns are unique, but the fact that you subsequently hard-code in another sort at the end leads me to believe that Field1 and Field2 are neither related nor unique, in which case you really should consider the possibility of having an arbitrary number of levels of sorting, perhaps by accepting an IEnumerable<SORT_TYPE> or params SORT_TYPE[] argument instead of a single SORT_TYPE.

    Anyway, as far as performance goes, the OrderBy and ThenBy extensions have deferred execution, so each successive ThenBy in your code is probably no more than a few CPU instructions, it’s just wrapping one function in another. It will be fine; the actual sorting will be far more expensive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method in my code: private bool GenerateZipFile(List<FileInfo> filesToArchive, DateTime archiveDate)
I have the following method: private JobCard PopulateObject(JobCard jc, DataRow dataRow) { PropertyInfo[] proplist
I have the following method and interface: public object ProcessRules(List<IRule> rules) { foreach(IRule rule
I have the following extension method that takes a List and converts it to
I have the following example array: $comparisons = array( 0 => array( 'method' =>
i'm trying to have the following structure get /something/id/apply => controller 1 post /something/id/apply
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method that is replacing a pound sign from the file
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.