Reputation: 886
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);
}
}
Upvotes: 17
Views: 13053
Reputation: 7608
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/
Upvotes: 13
Reputation: 430
If you're absolutely looking to use a list, try this:
int loc;
for(loc = 0; loc < people.Count && people[loc].Name.CompareTo(personToInsert.Name) < 0; loc++);
people.Insert(loc, personToInsert);
You can replace people[loc].Name.CompareTo(personToInsert.Name) < 0
with whatever condition you're testing for - and you can change the sign to make it descending instead of ascending. Like people[loc].Age < personToInsert.Age
for example would sort by age.
Upvotes: 5
Reputation: 9261
SortedList
is what you need.Create a StringComparer object and pass it to the constructor of the sortedlist.The elements are automatically sorted as new items are inserted.
StringComparer stringComp = StringComparer.CurrentCulture;
SortedList sl = new SortedList(stringComp);
sl.Add("B", "SECOND");
sl.Add("A", "FIRST");
sl.Add("C", "THIRD");
Upvotes: 2
Reputation: 18553
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
Upvotes: 13
Reputation: 56182
Take a look at SortedSet<T>
class. Simply use it instead of List<T>
.
Upvotes: 2