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

  • SEARCH
  • Home
  • 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 998981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:16:58+00:00 2026-05-16T07:16:58+00:00

I have a large unsorted list of items. Some items are important and need

  • 0

I have a large unsorted list of items. Some items are important and need to listed first, followed by unimportant items. The items should be sorted by name in the two groups. I have a solution, but I believe it can be optimized. First, it gets a list of important items. Then a list of everything else, then concatenates the results. Any suggestions on how to optimize this?

Here is a simplified version of the problem for LINQPad:

var doc = XDocument.Parse(@"
<items>
    <item id='a'>not important4</item>
    <item id='b'>important2</item>
    <item id='c'>not important2</item>
    <item id='d'>not important3</item>
    <item id='e'>important1</item>
    <item id='f'>not important1</item>
</items>");
// identify which items are important
string[] importantItemIDs = new string[] { "b", "e" };
var items = doc.Root.Elements("item");

// get a list of important items (inner join)
var importantList = from itemID in importantItemIDs
            from item in items
            orderby (string) item.Value
            where itemID == (string) item.Attribute("id")
            select item;

// get items that are not important items           
var notImportantList = items.Except(importantList).OrderBy(i => (string) i.Value);

// concatenate both sets of results into one list
var fullList = importantList.Concat(notImportantList);
fullList.Select(v => v.Value).Dump();

Here’s the correct output:

important1
important2
not important1
not important2
not important3
not important4
  • 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-16T07:16:58+00:00Added an answer on May 16, 2026 at 7:16 am

    One approach that immediately comes to mind is to utilize OrderBy as well as ThenBy to avoid querying the original data source multiple times. Something like:

    var list = items
           .OrderBy(i => importantItemIDs.Contains(i.Attribute("id") ? 0 : 1)
           .ThenBy(i => i.Value);
           .Select(i => i.Value);
    

    I’m not sure if the ternary operator is necessary there – I forget how OrderBy deals with boolean results. Shouldn’t be a major performance concern anyway and is potentially a bit clearer.

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

Sidebar

Related Questions

I have large list of the drop-down menu. Some of the option have very
I have large tables, contains DateTime columns to store exact time of some events
I have a large AVL Tree which I build some times during program from
I have large time to first byte for static content on IIS 7.5, it's
I have two containers (the actual container is flexible, unsorted vs sorted doesn't matter
I have large text files upon which all kinds of operations need to be
I have large json data which is contains hundreds thousands elements, now i need
I have large MySQL tables with hundreds of thousands of rows. I need to
I have large matrix, 4000x4000 I need to calculate local average of 11x11 window
I have large data files stored in S3 that I need to analyze. Each

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.