算法在C#中排序对象的列表算法、对象、列表

2023-09-04 23:25:12 作者:走,耍流氓去

我有我想排序C#.NET 3.5的对象列表,对象如下:

I have a list of object I wish to sort in C#.Net 3.5, the object is as follows

ID |名称| PARENT_ID

id | name | parent_id

1 |超人| NULL

1 | Superman | NULL

2 |蝙蝠侠| 3

2 | Batman | 3

3 |守望| 1

4 |金刚狼| 2

4 | Wolverine | 2

我知道你们当中有些人可能会觉得这很容易,但我需要对列表进行排序的基础上PARENT_ID,这是指向自己的索引(ID)在同一个表。

I know some of you might find this easy, but I need to sort the list based on the parent_id, which is pointing to its own index(id) in the same table.

所以,有人可以给我一个很好的算法,这个列表进行排序,而不循环一遍又一遍?我真的不能句话是那么好,所以我似乎无法给谷歌,我想正确的结果。

So can someone give me a good algorithm to sort this list without looping over and over again? I cannot really phrase it that well, therefore I can't seem to google the right results I wanted.

的IEnumerable或数据表解决方案的集合将是巨大的。

A collection of IEnumerable or DataTable solution would be great.

在此先感谢。

编辑:----------------新的例子

----------------NEW Example

ID |名称| PARENT_ID

id | name | parent_id

1 |顶端子| NULL

1 | TOP CHILD | NULL

2 |儿童C | 3

2 | Child C | 3

3 |儿童B | 4

3 | Child B | 4

4 |儿童A | 1

4 | Child A | 1

---->输出我要的是

----> The Output I want is

ID |名称| PARENT_ID

id | name | parent_id

1 |顶端子| NULL

1 | TOP CHILD | NULL

4 |儿童A | 1

4 | Child A | 1

3 |儿童B | 4

3 | Child B | 4

2 |儿童C | 3

2 | Child C | 3

---->如果我使用排序依据,或排序,结果我得到的是

----> If I use OrderBy or Sort, the result I get is

ID |名称| PARENT_ID

id | name | parent_id

1 |顶端子| NULL

1 | TOP CHILD | NULL

4 |儿童A | 1

4 | Child A | 1

2 |儿童C | 3

2 | Child C | 3

3 |儿童B | 4

3 | Child B | 4

- >解的不就是我真正想要的, 再次对不起,没有说清楚

--> Non of the solutions is what I really wanted, Sorry again for not being clear

希望这个例子更清晰。

推荐答案

在编辑:我想我得到你和比较器的样子:

after you edit: I think I get you and the comparer looks like:

public Int32 CompareTo(SuperHero right)
{
    if (this.ID == right.ID)
        return 0;

    return this.ParentID.CompareTo(right.ID);
}

在回应您的评论:

类是这样的:

public class SuperHero : IComparable<SuperHero>
{
    public Int32 ID { get; set; }
    public String Name { get; set; }
    public Int32 ParentID { get; set; }

    public SuperHero(Int32 id, String name, Int32 pid)
    {
        this.ID = id;
        this.Name = name;
        this.ParentID = pid;
    }

    public Int32 CompareTo(SuperHero right)
    {
        if (this.ID == right.ID)
            return 0;

        return this.ParentID.CompareTo(right.ID);
    }

    public override string ToString()
    {
        return this.Name;
    }
}

和使用它:

static void Main(string[] args)
{
    // create your list
    List<SuperHero> heroes = new List<SuperHero>();

    // populate it
    heroes.Add(new SuperHero(1, "Superman", 0));
    heroes.Add(new SuperHero(2, "Batman", 3));
    heroes.Add(new SuperHero(3, "Watchman", 1));
    heroes.Add(new SuperHero(4, "Wolverine", 2));

    foreach (SuperHero hero in heroes)
        Console.WriteLine(hero.ToString());
    Console.WriteLine();

    // sort it
    heroes.Sort();

    foreach (SuperHero hero in heroes)
        Console.WriteLine(hero.ToString());

    Console.ReadKey();
}

在.NET排序(快速排序)将使用比较器的列表进行排序。

The .NET sort (QuickSort) will use your comparer to sort the list.