亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

基于子列表對列表列表進行排序的方法

基于子列表對列表列表進行排序的方法

C#
藍山帝景 2021-11-28 16:34:17
我有一個由int數組組成的列表列表:(List<List<int[]>>)我想根據列表int第一個元素中數組中的第一個索引對列表列表進行排序。到目前為止,我的解決方案是:List<List<int[]>> myList = new List<List<int[]>> { new List<int[]> { new int[] { 1, 5 }, new int[] { 2, 4 } }, new List<int[]> { new int[] { 3, 4 }, new int[] { 0, 1 } } };myList.OrderByDescending(x => x.Max(y => y[0])).ToList();結果是第二個列表排在第一位,第一個排在第二位。但我不喜歡那個,因為性能是一個關鍵點,我不喜歡執行這個Max操作,因為它沒用。那么,有沒有更好的辦法呢?——編輯:我完成了使用:myList.OrderByDescending(x => x[0][0]).ToList();正如 CodeCaster 在評論中提出的那樣。這個在我的代碼中比 Aldert 提出的選項更快。但他的回答也值得一看。
查看完整描述

3 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

此代碼根據您傳遞給比較器的順序對 asc 或 desc 進行排序。它在元素上運行 O*1,以設置能夠進行比較的結構。我很想知道它是否適合你更快(我認為只適用于非常大的樹)。當您已經對所有內部列表進行排序時,您不需要保留幫助字典,然后您可以取最后一個元素。


using System;

using System.Collections.Generic;


namespace ConsoleApp1

{

class Program

    {


        static void Main(string[] args)

        {

            List<List<int>> mainList = new List<List<int>>();


            List<int> newList = new List<int>();



            Random rand = new Random();

            for (int i = 0; i < 30; i++)

            {

                int ra = rand.Next(200);


                if (i % 5  == 0)

                {

                    if (newList.Count > 0)

                    {

                        newList = new List<int>();

                        mainList.Add(newList);

                    }

                }

                newList.Add(ra);


            }


            mainList.Sort( new MaxComparer(mainList, false));


            foreach (List<int> oneL in mainList)

            {

                foreach (int oneInt in oneL)

                {

                    Console.Write(oneInt + " ");

                }

                Console.WriteLine();

            }


        }


        public class MaxComparer : IComparer<List<int>>

        {

            bool order = false;

            Dictionary<int, int> helper = new Dictionary<int, int>();

            public MaxComparer(List<List<int>> sortList, bool Order)

            {

                order = Order;


                foreach (List<int> oneL in sortList)

                {

                    int max = int.MinValue;

                    foreach (int oneInt in oneL)

                    {

                        if (max < oneInt) max = oneInt;

                    }

                    helper.Add(oneL.GetHashCode(), max);

                }

            }


            public int Compare(List<int> x, List<int> y)

            {

                return helper[x.GetHashCode()].CompareTo(helper[y.GetHashCode()]) * (order ? 1:-1);


            }

        }

  }

}


查看完整回答
反對 回復 2021-11-28
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

這是您通過二進制比較尋找的答案,它相當簡單,因為它從 sublint 和數組中取出第一個元素(正如您所尋找的那樣)。


using System;

using System.Collections.Generic;


namespace ConsoleApp1

{

    class Program

    {


        static void Main(string[] args)

        {

            List<List<int[]>> mainList = new List<List<int[]>>();




            Random rand = new Random();

            for (int i = 0; i < 30; i++)

            {

                List<int[]> subList = new List<int[]>();


                int limj = rand.Next(5);

                for (int j = 0; j < 5 + limj; j++)

                {

                    int limk = rand.Next(5);

                    int[] arrayInt = new int[limk + 5];

                    for (int k = 0; k < limk + 5; k++)

                    {

                        arrayInt[k] = rand.Next(200);

                    }

                    subList.Add(arrayInt);


                }

                mainList.Add(subList);


            }


            mainList.Sort(new MaxComparer(false));


            foreach (List<int[]> oneL in mainList)

            {

                foreach (int[] arrayList in oneL)

                {

                    foreach (int i in arrayList) Console.Write(i + " ");

                    Console.Write("|");

                }

                Console.WriteLine();

            }


        }


        public class MaxComparer : IComparer<List<int[]>>

        {

            bool order = false;

            public MaxComparer(bool Order)

            {

                order = Order;



            }


            public int Compare(List<int[]> x, List<int[]> y)

            {


                return x[0][0].CompareTo(y[0][0]) * (order ? 1 : -1);


            }

        }

    }

}



查看完整回答
反對 回復 2021-11-28
?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

這是你想要的?

 var sortedList = myList.OrderBy(x => x.Select(y => y.Select(z => z).OrderBy(z => z))).ToList();

編輯:我忘了深入一層。導致該錯誤的原因是它想要對數組對象而不是其元素進行排序。


查看完整回答
反對 回復 2021-11-28
  • 3 回答
  • 0 關注
  • 323 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號