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

Related Questions

I need a quick algorithm to select 5 random elements from a generic list.
I need to enumerate though generic IList<> of objects. The contents of the 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 have an abstract generic class BLL<T> where T : BusinessObject . I need
I need to create at runtime instances of a class that uses generics, like
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either
Need a way to allow sorting except for last item with in a list.

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.