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 3434382
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:41:30+00:00 2026-05-18T07:41:30+00:00

So I am getting some files like this: int pathLength = path.Length + 1;

  • 0

So I am getting some files like this:

int pathLength = path.Length + 1;
var files = Directory.GetFiles ( path, "*.*", SearchOption.AllDirectories )
    .Where ( s => s.EndsWith ( ".bmp", StringComparison.OrdinalIgnoreCase ) ||
        s.EndsWith ( ".jpg", StringComparison.OrdinalIgnoreCase ) )
        .Select ( s => s.Substring ( pathLength, s.Length - pathLength ) )

    .ToList ( );

and was before sorting like this:

FileComparer fileComparer = new FileComparer ( );
files.Sort ( fileComparer );

But for Sort I need to use ToList. I am wondering if I can just add the Sort to the Linq the same way and get rid of ToList?

  • 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-18T07:41:31+00:00Added an answer on May 18, 2026 at 7:41 am

    You’re looking for the OrderBy method:

    var extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".bmp", ".jpg" };
    int pathLength = path.Length + 1;
    var files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
        .Where(s => extensions.Contains(Path.GetExtension(s)))
        .Select(s => s.Substring(pathLength))
        .OrderBy(s => s, new FileComparer());
    

    You can replace your FileComparer class with

    .OrderBy(s => !s.Contains('\\')).ThenBy(s => s)
    

    Since false < true, this will sort strings that start with \\ before other ones.

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

Sidebar

Related Questions

I'm trying to rename some files, but getting a baffling error*. When I run
I am getting 'trailing whitespace' errors trying to commit some files in Git. I
What are some options for getting really fast 'Find in Files' searching in VIM?
I am getting this annoying problem, whenever i do some changes to any file
I have a .txt file looking like this: 1 Some string Some other string
I have some code that moves files around and would like to implement a
Is there some trick to getting a SpringMVC custom view to cause a file
I've inherited some aspx (including the web.config file) and am getting the following error.
I'm getting some strange, intermittent, data aborts (< 5% of the time) in some
I am getting some errors thrown in my code when I open a Windows

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.