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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:44:31+00:00 2026-05-11T01:44:31+00:00

I need a generic container that keeps its elements sorted and can be asked

  • 0

I need a generic container that keeps its elements sorted and can be asked where (at which position) it would insert a new element, without actually inserting it.

Does such a container exist in the .NET libraries? The best illustration is an example (container sorts characters by ASCII value, let’s assume unicode does not exist):

sortedContainer.Add('d'); sortedContainer.Add('b'); sortedContainer.Add('g');  //container contains elements ordered like 'b' 'd' 'g' //index  -------------------------------->  0   1   2  sortedContainer.GetSortedIndex('a'); //returns 0 sortedContainer.GetSortedIndex('b'); //returns 0  sortedContainer.GetSortedIndex('c'); //returns 1 sortedContainer.GetSortedIndex('d'); //returns 1  sortedContainer.GetSortedIndex('e'); //returns 2 sortedContainer.GetSortedIndex('f'); //returns 2 sortedContainer.GetSortedIndex('g'); //returns 2  sortedContainer.GetSortedIndex('h'); //returns 3 [...] 

The search for the position should take advantage of the fact that the elements are sorted.

  • 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. 2026-05-11T01:44:31+00:00Added an answer on May 11, 2026 at 1:44 am

    If you sort a List<T> and then use List<T>.BinarySearch it will give you the index of the entry if it exists, or the bitwise complement of the index of where it would be inserted if you inserted then sorted. From that, you should easily be able to build your method.

    Sample code matching your example, but not the results – if you look at your sample, you’ve only got 3 entries, so it doesn’t make sense for ‘h’ to return 4 or ‘g’ to return 3. I hope that’s your example which is slightly off, rather than me misunderstanding the problem 🙂 Note that the sorting isn’t automatic – you’d have to sort the list explicitly before calling GetSortedIndex.

    using System; using System.Collections.Generic;  static class Test {     static int GetSortedIndex<T>(this List<T> list, T entry)     {         int index = list.BinarySearch(entry);         return index >= 0 ? index : ~index;     }      static void Main()     {         List<char> container = new List<char> { 'b', 'd', 'g' };         Console.WriteLine(container.GetSortedIndex('a'));         Console.WriteLine(container.GetSortedIndex('b'));         Console.WriteLine(container.GetSortedIndex('c'));         Console.WriteLine(container.GetSortedIndex('d'));         Console.WriteLine(container.GetSortedIndex('e'));         Console.WriteLine(container.GetSortedIndex('f'));         Console.WriteLine(container.GetSortedIndex('g'));         Console.WriteLine(container.GetSortedIndex('h'));     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You could do it with a Timer, but you would… May 11, 2026 at 12:54 pm
  • added an answer You are creating a webAdressViewController for each url, but you… May 11, 2026 at 12:54 pm
  • added an answer For simple types like Point and Size you are better… May 11, 2026 at 12:54 pm

Related Questions

I need a quick algorithm to select 5 random elements from a generic list.
I need to develop a generic jQuery-based search plugin for the ASP.NET MVC application
I'm creating a generic class and in one of the methods I need to
I need to create at runtime instances of a class that uses generics, like
Having read up on quite a few articles on Artificial Life (A subject I
I tried to flag a collection property on a class as Obsolete to find
I'm writing a semi-generic form plugin using jQuery in order to speed up the
It looks like I had a fundamental misunderstanding about C++ :< I like the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.