簡體   English   中英

按字符串數組 c# 對列表進行排序

[英]Sort a List by a string array c#

我正在嘗試按列表中的其他值對列表進行排序。

當我試圖完成時:當我向列表添加一個值時,我將使用一個包含該值的方法,以及一個表示它的依賴項的值的字符串 []。

我想通過 ListObject 中的 string[] 中的值對列表進行排序

這是一個程序示例:

public class ListObject {
    private string name;
    private string[] dependencies;
    private object value;

    public ListObject(string name, string[] dependencies, object value) {
        this.name = name;
        this.dependencies = dependencies;
        this.value = value;
    }
}

public class ExampleClass {
    public static void Main(string[] args) {
        List<ListObject> list = new List<ListObject>();
        list.Add(new ListObject("ran", new string[] { }, "Value"));
        list.Add(new ListObject("far", new string[] {"thest"}, "Value"));
        list.Add(new ListObject("the", new string[] {"ran"}, "Value"));
        list.Add(new ListObject("thest", new string[] {"the", "ran"}, "Value"));
        list.Add(new ListObject("man", new string[] {"ran", "thest"}, "Value"));

        //What I want the order of the list to become
        /* ran
         * the
         * far
         * thest
         * man
         */
    }
}

更多信息:我在運行時生成類,我需要確保如果一個字段具有生成的 class 類型,那么我需要確保我生成 class 該字段依賴於在我生成位於 D 中的 ZA2F2ED4F8EBC2CBB4C21A29DC40AB61 之前。

我並不完全理解規則並且您更改了測試輸入,但這里是產生您想要的序列的版本,更一般地說是排序策略的所有排序(badam tsh.)的藍圖。

using System.Linq;

(...)

public class ListObject
    {
        public string name {get;} // Made into properties and public

(...)

       List<ListObject> list = new List<ListObject>();
       list.Add(new ListObject("ran", new string[] { }, "Value"));
       list.Add(new ListObject("far", new string[] {"thest"}, "Value"));
       list.Add(new ListObject("the", new string[] {"ran"}, "Value"));
       list.Add(new ListObject("thest", new string[] {"ran", "the"}, "Value"));

            //What I want the order of the list to become
            /* ran
             * the
             * far
             * thest
             */

        foreach(var o in list) { Console.WriteLine(o.name);}

        var ordered = list
            .OrderBy(o => o.dependencies.Count()) // This is not needed, it's here just to show that you can chain OrderBy
            .ThenBy( o => string.Join(",",o.dependencies));
        Console.WriteLine(">>>Sorted");
        foreach(var o in ordered) { Console.WriteLine(o.name);}

Output:

ran
far
the
thest
>>>Sorted
ran
the
far
thest

這是一個帶有 object 引用而不是字符串的版本

    public class ListObject
    {
        public string name { get; }

        public ListObject[] dependencies{get;}

        private object value;
        public ListObject(string name, ListObject[] dependencies, object value)
        {
            this.name = name;
            this.dependencies = dependencies;
            this.value = value;
        }
    }

    public static void Main(string[] args)
    {
        var ran = new ListObject("ran", new ListObject[0], "Value");
        var the = new ListObject("the", new[]{ran}, "Value");
        var thest = new ListObject("thest", new[]{ran, the}, "Value");
        var far = new ListObject("far", new[]{thest}, "Value");

        List<ListObject> list = new List<ListObject>(){ran, the, thest, far};
        //What I want the order of the list to become
        /* ran
             * the
             * far
             * thest
             */
        foreach (var o in list)
        {
            Console.WriteLine(o.name);
        }

        var ordered = list
              .OrderBy(o => o.dependencies.Count()) // This is not needed, it's here just to show that you can chain OrderBy
              .ThenBy(o => string.Join(",", o.dependencies.Select(d => d.name)));
        Console.WriteLine(">>>Sorted");
        foreach (var o in ordered)
        {
            Console.WriteLine(o.name);
        }
    }

在以下位置找到了我的問題的解決方案: http://tawani.blogspot.com/2009/02/topological-sorting-and-cyclic.html

它被稱為拓撲排序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM