簡體   English   中英

嘗試創建一種方法,按小寫首字母升序排序句子,然后按大寫首字母降序排序

[英]Trying to create method that sorts a sentence in ascending order by lower case first letter then descending by upper case first letter

我正在嘗試編寫一個 C# 方法,該方法接受句子“The quick brown Fox jumped over the lazy Dog”並按首字母升序對其進行排序,然后在末尾按降序對任何大寫單詞進行排序。 結果應該是這樣的......“棕色的狐狸狗很快就懶洋洋地跳了過去”。

我被困在如何將三個大寫單詞添加到空列表中。 到目前為止,我的代碼識別了大寫的單詞,但無法弄清楚如何將這些單詞移動到空列表中。 我嘗試了幾種方法,包括.Add()。 我可能會弄錯語法。

我的邏輯是我將創建第二個包含大寫單詞的列表,對兩個列表進行排序然后將它們連接起來。 這是我在這里的第一篇文章,所以如果事情沒有像他們應該的那樣清楚,我提前道歉。 對此的任何幫助都會很棒。 謝謝

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string words = "The quick brown Fox jumped over the lazy Dog.";
            List<string> wordList = words.Split(' ').ToList();
            List<string> upperList = new List<string>();

            foreach (string word in wordList)
            {
                if (Char.IsUpper(word, 0))
                {
                    
                    Console.WriteLine(word);
                }
            }


            Console.ReadLine();
        }
    }
}

您可以使用 LINQ:

List<string> wordList = words.Split(' ').
    OrderBy(word => char.IsUpper(word[0])).
    ThenBy(word => word).ToList();

在線演示: https://dotnetfiddle.net/f59eUt

暫無
暫無

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

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