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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:15:48+00:00 2026-05-24T13:15:48+00:00

Is possible to order on Linq according to a specific order? something like List<bbHeader>

  • 0

Is possible to order on Linq according to a specific order?
something like

List<bbHeader> bb = new List<bbHeader>();
bb.OrderBy(x => x.Country.CompareTo(new string[]{"AR","CL","PY","UY","AUP"}));

The idea is that the Country field is ordered according to the specific order of the string

  • 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-24T13:15:49+00:00Added an answer on May 24, 2026 at 1:15 pm

    There’s a very direct way in your example:

    var sequence = new [] { "AR", "CL", "PY", "UY", "AUP" };
    
    List<bbHeader> bb = new List<bbHeadher>();
    
    // fill bb
    
    // takes the item, checks the index of the country in the array
    var result = bb.OrderBy(x => Array.IndexOf(sequence, x.Country));
    

    In this way, you are ordering by the index Country is found in the sequence string. Just keep in mind that not-found items will be -1, which you can correct for also if you like.

    If you want to do anything more complex, you can to create your own custom IComparer class implementation to compare the items using your custom order. This can then be passed into OrderBy.

    Such an IComparer would look like:

    public sealed class SequenceComparer : IComparer<string>
    {
        private string[] _sequence { get; set; }
    
        public SequenceComparer(string[] sequence)
        {
            if (sequence == null) throw new ArgumentNullException("sequence");
    
            _sequence = sequence;
        }
    
        public int Compare(string x, string y)
        {
            if (ReferenceEquals(x, y)) return 0;
    
            return Array.IndexOf(_sequence, x).CompareTo(Array.IndexOf(_sequence, y));
        }
    }
    

    And can be called like:

    var result = bb.OrderBy(x => x.Country, new SequenceComparer(new [] { "AR", "CL", "PY", "UY", "AUP" }));
    

    Either way works well, the latter is nice and reusable, but the former (using IndexOf directly) is still very concise as well. Your choice.

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

Sidebar

Related Questions

Possible Duplicates: Sorting a list using Lambda/Linq to objects C# List<> OrderBy Alphabetical Order
Is it possible to use LINQ OrderBy like so: .OrderBy(x=>(x.SourceID == 3), (x.SourceID ==
In LINQ, is it possible to have conditional orderby sort order (ascending vs. descending).
How could I do something like this in Linq to SQL (VB.NET if possible)?
Is it possible to change the order of parameters to sprintf ? like sprintf(
I have a list of string tuples, say (P1,P2) I'd like to know if
I have a problem with LINQ (in C#). I need to order a list
I'd like to expose a method over WCF something like GetEmployees(EmployeeColumn orderby), that returns
Is it possible to create some Linq that generates a List containing all possible
Is it possible to do common table expressions (CTE) (like shown below) in Linq

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.