当前位置: 首页>>代码示例>>C#>>正文


C# SortedList.OrderByDescending方法代码示例

本文整理汇总了C#中SortedList.OrderByDescending方法的典型用法代码示例。如果您正苦于以下问题:C# SortedList.OrderByDescending方法的具体用法?C# SortedList.OrderByDescending怎么用?C# SortedList.OrderByDescending使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SortedList的用法示例。


在下文中一共展示了SortedList.OrderByDescending方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetCount

        public FreqResult GetCount(IEnumerable<string> words)
        {
            var uniqueWords = new SortedList<string, int>();
            foreach (var word in words)
            {
                if (uniqueWords.ContainsKey(word))
                {
                    uniqueWords[word]++;
                }
                else
                {
                    uniqueWords.Add(word, 1);
                }
            }

            var result = uniqueWords.OrderByDescending(pair => pair.Value).ToArray();
            return new FreqResult(uniqueWords.Count, result.Take(10));
        }
开发者ID:mokeev1995,项目名称:LaboratoryWorks,代码行数:18,代码来源:SortedListFreq.cs

示例2: BruteForceMainNoTrades

        /// <summary>
        /// Tries all possibilities for main teams, picks the best one
        /// </summary>
        /// <param name="limit">Number of players to consider</param>
        public void BruteForceMainNoTrades(int limit)
        {
            errorwriter.Write("Starting brute force solution with " + limit + " players");

            string selectedTeam = string.Empty;
            string[] selectedPlayers = new string[6];
            int totalPoints = 0;

            // Sort players by point total
            SortedList<string, int> playerPointComparison = new SortedList<string, int>();
            for (int i = 0; i < playerList.Count; i++)
            {
                playerPointComparison.Add(playerList.Keys.ElementAt(i), playerList.ElementAt(i).Value.TotalPoints);
            }

            var playerPointSorted = playerPointComparison.OrderByDescending(l => l.Value);

            if (limit > playerPointSorted.Count())
            {
                limit = playerPointSorted.Count();
            }

            // Sort players by cost
            SortedList<string, int> playerCostComparison = new SortedList<string, int>();
            for (int i = 0; i < limit; i++)
            {
                playerCostComparison.Add(playerList.Keys.ElementAt(i), playerList.ElementAt(i).Value.Cost);
            }

            var playerCostSorted = playerCostComparison.OrderByDescending(l => l.Value);

            // The goal is to iterate through the list of players in order of increasing cost
            // Thus, when the cost goes over the allowed limit, we can safely ignore all other entries in the list

            string[] players = new string[6];

            int tcost = 0;
            int p1cost = 0;
            int p2cost = 0;
            int p3cost = 0;
            int p4cost = 0;
            int p5cost = 0;
            int p6cost = 0;
            int possibleTeams = 0;

            // Loop for team
            for (int i0 = 0; i0 < teamList.Count; i0++)
            {
                string team = teamList.ElementAt(i0).Key;
                tcost = teamList[team].Cost;

                ProgressUpdate(i0 * 100 / teamList.Count());

                // Nest loops for all six players
                for (int i1 = playerCostSorted.Count() - 1; i1 >= 5; i1--)
                {
                    players[0] = playerCostSorted.ElementAt(i1).Key;
                    p1cost = playerList[players[0]].Cost;
                    if (DoNotDraftFailed(players[0], true))
                    {
                        continue;
                    }

                    for (int i2 = i1 - 1; i2 >= 4; i2--)
                    {
                        players[1] = playerCostSorted.ElementAt(i2).Key;
                        if (DoNotDraftFailed(players[1], true))
                        {
                            continue;
                        }

                        p2cost = playerList[players[1]].Cost;

                        for (int i3 = i2-1; i3 >= 3; i3--)
                        {
                            players[2] = playerCostSorted.ElementAt(i3).Key;
                            if (DoNotDraftFailed(players[2], true))
                            {
                                continue;
                            }

                            p3cost = playerList[players[2]].Cost;
                            if (tcost + p1cost + p2cost + p3cost > MAINCOSTLIMIT)
                            {
                                break;
                            }

                            for (int i4 = i3 - 1; i4 >= 2; i4--)
                            {
                                players[3] = playerCostSorted.ElementAt(i4).Key;
                                if (DoNotDraftFailed(players[3], true))
                                {
                                    continue;
                                }

                                p4cost = playerList[players[3]].Cost;
//.........这里部分代码省略.........
开发者ID:spazer,项目名称:FPL_Calculator,代码行数:101,代码来源:Stats.cs

示例3: BestTeamGenerationWithTrades

        public void BestTeamGenerationWithTrades(int maxPlayers)
        {
            errorwriter.Write("Starting brute force solution with trades");

            if (maxPlayers > playerList.Count)
            {
                maxPlayers = playerList.Count;
            }

            List<string> bestPlayers = FindBestPlayersPerWeek(maxWeeks, maxPlayers);

            errorwriter.Write("Picking best " + maxPlayers + " players per week. " + bestPlayers.Count + " players total.");

            string[] selectedTeam = new string[maxWeeks];
            string[][] selectedPlayers = new string[maxWeeks][];
            string[] team = new string[6];
            string[][] players = new string[maxWeeks][];
            string[] potentialTeam = new string[6];
            string[][] potentialPlayers = new string[maxWeeks][];
            int bestResult = 0;

            for (int i=0; i<maxWeeks;i++)
            {
                selectedPlayers[i] = new string[6];
                players[i] = new string[6];
                potentialPlayers[i] = new string[6];
            }

            errorwriter.Write("Sorting by point gain per week");

            // Sort players by point gain per week
            SortedList<string, int> playerPointComparison = new SortedList<string, int>();
            for (int i = 0; i < bestPlayers.Count; i++)
            {
                playerPointComparison.Add(bestPlayers[i], playerList.ElementAt(i).Value.TotalPoints);
            }

            var playerPointSorted = playerPointComparison.OrderByDescending(l => l.Value);

            errorwriter.Write("Sorting by cost");

            // Sort players by cost
            SortedList<string, int> playerCostComparison = new SortedList<string, int>();
            for (int i = 0; i < bestPlayers.Count; i++)
            {
                playerCostComparison.Add(playerPointSorted.ElementAt(i).Key, playerList[playerPointSorted.ElementAt(i).Key].Cost);
            }

            var playerCostSorted = playerCostComparison.OrderByDescending(l => l.Value);

            errorwriter.Write("Sorting by trade value");

            // Sort players by trade value
            SortedList<string, double>[] playerTradeComparison = new SortedList<string, double>[maxWeeks];
            for (int i = 0; i < maxWeeks; i++)
            {
                playerTradeComparison[i] = new SortedList<string, double>();
                for (int j = 0; j < bestPlayers.Count; j++)
                {
                    playerTradeComparison[i].Add(playerPointSorted.ElementAt(j).Key, playerList[playerPointSorted.ElementAt(j).Key].AdjustedTradeValue(i+1));
                }
            }

            IOrderedEnumerable<KeyValuePair<string, double>>[] playerTradeSorted = new IOrderedEnumerable<KeyValuePair<string,double>>[maxWeeks];
            for (int i = 0; i < maxWeeks; i++)
            {
                playerTradeSorted[i] = playerTradeComparison[i].OrderByDescending(l => l.Value);
            }

            // Sort teams by trade value
            SortedList<string, double>[] teamTradeComparison = new SortedList<string, double>[maxWeeks];
            for (int i = 0; i < maxWeeks; i++)
            {
                teamTradeComparison[i] = new SortedList<string,double>();
                for (int j = 0; j < 6; j++)
                {
                    teamTradeComparison[i].Add(teamList.ElementAt(j).Key, teamList.ElementAt(j).Value.AdjustedTradeValue(i+1));
                }
            }

            IOrderedEnumerable<KeyValuePair<string, double>>[] teamTradeSorted = new IOrderedEnumerable<KeyValuePair<string, double>>[6];
            for (int i = 0; i < maxWeeks; i++)
            {
                teamTradeSorted[i] = teamTradeComparison[i].OrderByDescending(l => l.Value);
            }

            // The goal is to iterate through the list of players in order of increasing cost
            // Thus, when the cost goes over the allowed limit, we can safely ignore all other entries in the list
            // and continue the loop
            int tcost = 0;
            int p1cost = 0;
            int p2cost = 0;
            int p3cost = 0;
            int p4cost = 0;
            int p5cost = 0;
            int p6cost = 0;

            // Loop for team
            for (int i0 = 0; i0 < teamList.Count; i0++)
            {
//.........这里部分代码省略.........
开发者ID:spazer,项目名称:FPL_Calculator,代码行数:101,代码来源:Stats.cs

示例4: BestMainWithoutTrades

        /// <summary>
        /// Determines the highest scoring team without trades through algorithm use
        /// </summary>
        public void BestMainWithoutTrades()
        {
            string selectedTeam;
            string[] selectedPlayers = new string[6];
            Dictionary<chosenTeam, int> solutions = new Dictionary<chosenTeam, int>();

            // Sort players by point total
            SortedList<string, int> playerPointComparison = new SortedList<string, int>();
            for (int i = 0; i < playerList.Count; i++)
            {
                playerPointComparison.Add(playerList.Keys.ElementAt(i), playerList.ElementAt(i).Value.TotalPoints);
            }

            var playerPointSorted = playerPointComparison.OrderByDescending(l => l.Value);

            // Iterate through the list with each team since there's only eight of them
            errorwriter.Write("Calculating best team with no trades");
            for (int i = 0; i < teamList.Count; i++)
            {
                selectedTeam = teamList.ElementAt(i).Key;
                errorwriter.Write("Using " + selectedTeam);

                // Take the highest scoring players
                for (int j = 0; j < 6; j++)
                {
                    selectedPlayers[j] = playerPointSorted.ElementAt(j).Key;
                }

                // The next player in playerPointSorted
                int index = 5;

                // The cost of the team needs to be reduced to 30 or less
                while (MainCostRequirementCheck(selectedPlayers, selectedTeam) > 0 && index < playerList.Count)
                {
                    // Find the player with the lowest point to cost ratio
                    int lowestCostBenefitPlayer = 0;
                    for (int j = 1; j < 6; j++)
                    {
                        if (playerList[selectedPlayers[j]].PointToCostRatio < playerList[selectedPlayers[lowestCostBenefitPlayer]].PointToCostRatio)
                        {
                            lowestCostBenefitPlayer = j;
                        }
                    }

                    // Replace the lowest cost benefit player with the next player in the list, but only if their cost is lower
                    while (true)
                    {
                        index++;

                        if (index >= playerList.Count)
                        {
                            errorwriter.Write("Error during best team determination: index became greater than playerList.Count");
                            break;
                        }

                        string targetPlayer = playerPointSorted.ElementAt(index).Key;
                        if (playerList[targetPlayer].Cost < playerList[selectedPlayers[lowestCostBenefitPlayer]].Cost)
                        {
                            errorwriter.Write("Swapping " + selectedPlayers[lowestCostBenefitPlayer] + " with " + targetPlayer + " (Reducing cost)");
                            selectedPlayers[lowestCostBenefitPlayer] = targetPlayer;
                            break;
                        }
                    }
                }

                // Replace players with lower cost equivalents if available
                for (int j = 0; j < 6; j++)
                {
                    for (int k = 0; k < playerPointSorted.Count(); k++)
                    {
                        if (playerPointSorted.ElementAt(k).Value == playerList[selectedPlayers[j]].TotalPoints)
                        {
                            // Note that replacing a player with the same player is impossible since their costs will be the same
                            if (playerList[playerPointSorted.ElementAt(k).Key].Cost < playerList[selectedPlayers[j]].Cost)
                            {
                                bool duplicate = false;

                                // Avoid duplicates
                                for (int l = 0; l < 6; l++)
                                {
                                    if (playerPointSorted.ElementAt(k).Key == selectedPlayers[l])
                                    {
                                        duplicate = true;
                                    }
                                }

                                if (!duplicate)
                                {
                                    errorwriter.Write("Swapping " + selectedPlayers[j] + " with " + playerPointSorted.ElementAt(k).Key + " (Minimizing cost)");
                                    selectedPlayers[j] = playerPointSorted.ElementAt(k).Key;
                                }
                            }
                        }
                        else if (playerPointSorted.ElementAt(k).Value < playerList[selectedPlayers[j]].TotalPoints)
                        {
                            break;
                        }
//.........这里部分代码省略.........
开发者ID:spazer,项目名称:FPL_Calculator,代码行数:101,代码来源:Stats.cs

示例5: BestAntiGenerationWithTrades

        public void BestAntiGenerationWithTrades(int maxPlayers)
        {
            errorwriter.Write("Starting brute force anti with trades");

            if (maxPlayers > playerList.Count)
            {
                maxPlayers = playerList.Count;
            }

            List<string> bestPlayers = FindWorstPlayersPerWeek(maxWeeks, maxPlayers);

            errorwriter.Write("Picking worst " + maxPlayers + " players per week. " + bestPlayers.Count + " players total.");

            string[][] selectedPlayers = new string[maxWeeks][];
            string[][] players = new string[maxWeeks][];
            string[][] potentialPlayers = new string[maxWeeks][];
            int bestResult = -99;

            for (int i = 0; i < maxWeeks; i++)
            {
                selectedPlayers[i] = new string[3];
                players[i] = new string[3];
                potentialPlayers[i] = new string[3];
            }

            errorwriter.Write("Sorting by point gain per week");

            // Sort players by point gain per week
            SortedList<string, int>[] playerPointComparison = new SortedList<string, int>[maxWeeks];
            for (int i = 0; i < maxWeeks; i++)
            {
                playerPointComparison[i] = new SortedList<string, int>();
                for (int j = 0; j < bestPlayers.Count; j++)
                {
                    playerPointComparison[i].Add(bestPlayers[j], playerList.ElementAt(j).Value.Points(i));
                }
            }

            IOrderedEnumerable<KeyValuePair<string, int>>[] playerPointSorted = new IOrderedEnumerable<KeyValuePair<string, int>>[maxWeeks];
            for (int i = 0; i < maxWeeks; i++)
            {
                playerPointSorted[i] = playerPointComparison[i].OrderByDescending(l => l.Value);
            }

            errorwriter.Write("Sorting by cost");

            // Sort players by cost
            SortedList<string, int> playerCostComparison = new SortedList<string, int>();
            for (int i = 0; i < bestPlayers.Count; i++)
            {
                playerCostComparison.Add(bestPlayers[i], playerList[bestPlayers[i]].Cost);
            }

            var playerCostSorted = playerCostComparison.OrderByDescending(l => l.Value);

            errorwriter.Write("Sorting by trade value");

            // Sort players by trade value
            SortedList<string, double>[] playerTradeComparison = new SortedList<string, double>[maxWeeks];
            for (int i = 0; i < maxWeeks; i++)
            {
                playerTradeComparison[i] = new SortedList<string, double>();
                for (int j = 0; j < bestPlayers.Count; j++)
                {
                    playerTradeComparison[i].Add(bestPlayers[j], playerList[bestPlayers[j]].AdjustedTradeValue(i+1));
                }
            }

            IOrderedEnumerable<KeyValuePair<string, double>>[] playerTradeSorted = new IOrderedEnumerable<KeyValuePair<string, double>>[maxWeeks];
            for (int i = 0; i < maxWeeks; i++)
            {
                playerTradeSorted[i] = playerTradeComparison[i].OrderByDescending(l => l.Value);
            }

            // The goal is to iterate through the list of players in order of increasing cost
            // Thus, when the cost goes over the allowed limit, we can safely ignore all other entries in the list
            // and continue the loop
            int p1cost = 0;
            int p2cost = 0;
            int p3cost = 0;

            // Nest loops for all three players
            for (int i1 = playerCostSorted.Count() - 1; i1 >= 2; i1--)
            {
                ProgressUpdate((playerCostSorted.Count() - i1) * 100 / playerCostSorted.Count());

                players[0][0] = playerCostSorted.ElementAt(i1).Key;
                p1cost = playerList[players[0][0]].Cost;
                if (DoNotDraftFailed(players[0][0], true))
                {
                    continue;
                }

                for (int i2 = i1 - 1; i2 >= 1; i2--)
                {
                    players[0][1] = playerCostSorted.ElementAt(i2).Key;
                    if (DoNotDraftFailed(players[0][1], true))
                    {
                        continue;
                    }
//.........这里部分代码省略.........
开发者ID:spazer,项目名称:FPL_Calculator,代码行数:101,代码来源:Stats.cs

示例6: FindWorstPlayersPerWeek

        private List<string> FindWorstPlayersPerWeek(int weeks, int maxPlayers)
        {
            List<string> results = new List<string>();
            for (int w = 0; w < weeks; w++)
            {
                // Sort players by points
                SortedList<string, int> playerPointComparison = new SortedList<string, int>();
                for (int i = 0; i < playerList.Count; i++)
                {
                    playerPointComparison.Add(playerList.Keys.ElementAt(i), playerList.ElementAt(i).Value.Points(w));
                }

                var sortedplayers = playerPointComparison.OrderByDescending(l => l.Value);

                for (int i = playerPointComparison.Count - 1; i >= playerPointComparison.Count - maxPlayers; i--)
                {
                    if (!results.Contains(sortedplayers.ElementAt(i).Key))
                    {
                        results.Add(sortedplayers.ElementAt(i).Key);
                    }
                }
            }

            return results;
        }
开发者ID:spazer,项目名称:FPL_Calculator,代码行数:25,代码来源:Stats.cs


注:本文中的SortedList.OrderByDescending方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。