第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

基于子列表對(duì)列表列表進(jìn)行排序的方法

基于子列表對(duì)列表列表進(jìn)行排序的方法

C#
藍(lán)山帝景 2021-11-28 16:34:17
我有一個(gè)由int數(shù)組組成的列表列表:(List<List<int[]>>)我想根據(jù)列表int第一個(gè)元素中數(shù)組中的第一個(gè)索引對(duì)列表列表進(jìn)行排序。到目前為止,我的解決方案是: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();結(jié)果是第二個(gè)列表排在第一位,第一個(gè)排在第二位。但我不喜歡那個(gè),因?yàn)樾阅苁且粋€(gè)關(guān)鍵點(diǎn),我不喜歡執(zhí)行這個(gè)Max操作,因?yàn)樗鼪]用。那么,有沒有更好的辦法呢?——編輯:我完成了使用:myList.OrderByDescending(x => x[0][0]).ToList();正如 CodeCaster 在評(píng)論中提出的那樣。這個(gè)在我的代碼中比 Aldert 提出的選項(xiàng)更快。但他的回答也值得一看。
查看完整描述

3 回答

?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

此代碼根據(jù)您傳遞給比較器的順序?qū)?asc 或 desc 進(jìn)行排序。它在元素上運(yùn)行 O*1,以設(shè)置能夠進(jìn)行比較的結(jié)構(gòu)。我很想知道它是否適合你更快(我認(rèn)為只適用于非常大的樹)。當(dāng)您已經(jīng)對(duì)所有內(nèi)部列表進(jìn)行排序時(shí),您不需要保留幫助字典,然后您可以取最后一個(gè)元素。


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


            }

        }

  }

}


查看完整回答
反對(duì) 回復(fù) 2021-11-28
?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊

這是您通過二進(jìn)制比較尋找的答案,它相當(dāng)簡(jiǎn)單,因?yàn)樗鼜?sublint 和數(shù)組中取出第一個(gè)元素(正如您所尋找的那樣)。


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


            }

        }

    }

}



查看完整回答
反對(duì) 回復(fù) 2021-11-28
?
Cats萌萌

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊

這是你想要的?

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

編輯:我忘了深入一層。導(dǎo)致該錯(cuò)誤的原因是它想要對(duì)數(shù)組對(duì)象而不是其元素進(jìn)行排序。


查看完整回答
反對(duì) 回復(fù) 2021-11-28
  • 3 回答
  • 0 關(guān)注
  • 304 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)