【问题标题】:insert into a List alphabetically C#按字母顺序插入列表 C#
【发布时间】:2012-10-21 16:12:57
【问题描述】:

谁能教我如何在 C# 中按字母顺序将项目插入列表中?

所以每次我添加到列表中时,我想按字母顺序添加一个项目,理论上列表可能会变得非常大。

示例代码:

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);
    }

}

【问题讨论】:

  • 在这个 Stackoverflow 中:stackoverflow.com/questions/188141/…
  • @John3136 在合适的位置插入和每次插入后重新排序列表是不一样的。
  • 使用专门的集合 - 例如排序列表。 List.Insert 在 LOC、错误、cpu、内存、程序员时间等方面并不是一个有效的解决方案。
  • Kirk - 我看不出我建议它在哪里。我的目的是找到现有的排序集合类之一,而不是找出如何对集合进行排序。

标签: c# .net


【解决方案1】:

定义一个实现IComparer&lt;T&gt; Interface的比较器:

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

然后使用SortedSet&lt;T&gt; Class

        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);

如果您仅限于使用 .NetFramework3.5,则可以使用 SortedList&lt;TKey, TValue&gt; Class 然后:

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);

特别是阅读 MSDN 文章中的 Remarks 部分,比较这个类和SortedDictionary&lt;TKey, TValue&gt; Class

【讨论】:

    【解决方案2】:

    旧线程,但该线程 IMO 中的答案忽略了 OP 的实际问题。问题很简单——如何按排序顺序插入列表。这与“仅使用 SortedSet / SortedList”不同。使用以下内容与使用 SortedList 会有不同的特征和含义。

    SortedSet 和 SortedList 都基于 Dictionary,并且不允许您添加具有相同密钥 AFAIK 的两个项目。

    那么,您如何解释 { a, b, c, c, d } 之类的列表?

    这是插入有序列表以使项目保持有序的正确方法:

    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);
    }
    

    通过 2010 年的这篇精彩文章回答:https://debugmode.net/2010/09/18/inserting-element-in-sorted-generic-list-list-using-binary-search/

    【讨论】:

      【解决方案3】:

      如果您绝对希望使用列表,请尝试以下操作:

      int loc;
      for(loc = 0; loc < people.Count && people[loc].Name.CompareTo(personToInsert.Name) < 0; loc++);
      people.Insert(loc, personToInsert);
      

      您可以将people[loc].Name.CompareTo(personToInsert.Name) &lt; 0 替换为您正在测试的任何条件 - 您可以更改符号以使其降序而不是升序。比如people[loc].Age &lt; personToInsert.Age 会按年龄排序。

      【讨论】:

      • 虽然这对于大多数列表来说不是最有效的排序插入,但对于非常小的列表(例如:少于 10 个元素),这可能比执行二分查找要快。就个人而言,我会使用while 而不是for(但这没关系)。
      • 这对我来说很好,谢谢。我只需要一种简单的轻量级方法将新项目插入到绑定到可观察集合的下拉列表中的正确位置。
      【解决方案4】:

      看看SortedSet&lt;T&gt; 类。只需使用它来代替List&lt;T&gt;

      【讨论】:

        【解决方案5】:

        SortedList 是你所需要的。创建一个 StringComparer 对象并将其传递给 sortedlist 的构造函数。元素在插入新项目时自动排序。

        StringComparer stringComp = StringComparer.CurrentCulture;
        SortedList sl = new SortedList(stringComp);
        sl.Add("B", "SECOND");
        sl.Add("A", "FIRST");
        sl.Add("C", "THIRD");
        

        【讨论】:

          猜你喜欢
          • 2019-01-25
          • 2018-08-01
          • 2013-09-17
          • 1970-01-01
          • 2019-09-23
          • 2020-03-29
          • 2017-05-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多