Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert into a List alphabetically C#

Tags:

c#

.net

Could anyone please teach me how to insert item into list in alphabetical order in C#?

So every time I add to the list I want to add an item alpabetically, the list could become quite large in theory.

Sample Code:

Public Class Person
{
     public string Name { get; set; }
     public string Age { get; set; }
}

Public Class Storage
{
    private List<Person> people;

    public Storage
    {
        people = new List<Person>();
    }


    public void addToList(person Person)
    {
        int insertIndex = movies.findindex(
            delegate(Movie movie) 
            {
              return //Stuck here, or Completely off Track.

            }
        people.insert(insertIndex, newPerson);
    }

}
like image 283
Jaybeecave Avatar asked Nov 01 '12 04:11

Jaybeecave


2 Answers

Define a comparer implemeting IComparer<T> Interface:

public class PersonComparer : IComparer<Person>
{
    public int Compare(Person x, Person y)
    {
        return x.Name.CompareTo(y.Name);
    }
}

And use SortedSet<T> Class then:

        SortedSet<Person> list = new SortedSet<Person>(new PersonComparer());
        list.Add(new Person { Name = "aby", Age = "1" });
        list.Add(new Person { Name = "aab", Age = "2" });
        foreach (Person p in list)
            Console.WriteLine(p.Name);

If you are limited to usinf .NetFramework3.5, you could use SortedList<TKey, TValue> Class then:

SortedList<string, Person> list = 
          new SortedList<string, Person> (StringComparer.CurrentCulture);
Person person = new Person { Name = "aby", Age = "1" };
list.Add(person.Name, person);
person = new Person { Name = "aab", Age = "2" };
list.Add(person.Name, person);

foreach (Person p in list.Values)
    Console.WriteLine(p.Name);

Espesially read the Remarks section in the MSDN artcile, comparing this class and SortedDictionary<TKey, TValue> Class

like image 197
horgh Avatar answered Nov 02 '22 08:11

horgh


Old thread, but the answers in this thread IMO are ignoring OP's actual question. The question is straightforward - how do you insert into a list in sorted order. That's not the same as "just use a SortedSet / SortedList". There will be different characteristics and implications based on using the below vs. using a SortedList.

SortedSet and SortedList are both based off of Dictionary, and won't allow you to add two items with the same key AFAIK.

So how do you account for a list such as, { a, b, c, c, d }?

Here is the correct way to insert into an ordered list so that the items remain ordered:

var binarySearchIndex = list.BinarySearch(item, itemComparer);
//The value will be a negative integer if the list already 
//contains an item equal to the one searched for above
if (binarySearchIndex < 0)
{
    list.Insert(~binarySearchIndex, item);
}
else
{
    list.Insert(binarySearchIndex, item);
}

Answer via this great article from 2010: https://debugmode.net/2010/09/18/inserting-element-in-sorted-generic-list-list-using-binary-search/

like image 11
Arash Emami Avatar answered Nov 02 '22 09:11

Arash Emami