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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:37:03+00:00 2026-06-07T16:37:03+00:00

I am implementing a Search autocomplete. I’d like to return a list of results

  • 0

I am implementing a “Search” autocomplete. I’d like to return a list of results sorted by the ordering of the submitted characters by the user where items at the top of the sorted results are those items that have the characters in the string in the first to n characters.

Is there an elegant way to do this using LINQ?

For example, assume that when the user types “un” the following items are returned from the database.

  • Texas, United States
  • Florida, United States
  • New York, United States
  • United States

I would like to return the ordered result for display as follows:

  1. United States
  2. Texas, United States
  3. Florida, United States
  4. New York, United States

Notice that for each of the strings above “un” is in position 1-2, 7-8, 10-11 and 11-12.

  • 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-06-07T16:37:05+00:00Added an answer on June 7, 2026 at 4:37 pm

    Generally the answer from @HugoRune will work, but it’s a bit inefficient having two issues.

    1. Using ToLower() leads all strings to converted first to lowercase, even if the comparison of firts char shows that they do not match.

    2. We perform basically the same comparison two times. Once with Contains() and once with IndexOf(). So we run through string two times.

    Following code is at least twice as efficient:

            IEnumerable<string> stateNames= [your source here];
            string searchString="un";
    
            var result = 
                stateNames
                    .Select(state=> new {Name=state, Index = state.IndexOf(searchString, StringComparison.CurrentCultureIgnoreCase)})
                    .Where(tuple=>tuple.Index>=0)
                    .OrderBy(tuple=>tuple.Index)
                    .Select(tuple=>tuple.Name);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application i am implementing search, it is like when user enter text
I am implementing search page in php. I need to show the results like
I'm implementing a search before data entry, to make sure the user is not
I am implementing a search field where the user can type in a string
I'm implementing global search box extension (sth like SearchableDictionary sample in android sdk). Everything
I'm implementing a book search function based on authors. I should return a query
I am implementing a jquery autocomplete on a search form and am getting the
I'm implementing a search in my website, and would like to support searching for
I'm implementing an autocomplete that will allow a user to enter in partial text
I'm implementing a Google Suggest like autocomplete feature for tag searching using jQuery's autocomplete.

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.