简体   繁体   English

如何在C#中从最大数量到最小数量的这种类型的元素对字符串数组进行排序?

[英]How to sort a string array from maximum to minimum amount of that type of elements in c#?

I am sorry for confusing title, how do i make an array of these elements : 对不起,标题令人困惑,我该如何排列这些元素:

string [] anArray = new string[50];
anArray[0] = "New York";
anArray[1] = "London";
anArray[2] = "New York";
anArray[3] = "London";
anArray[4] = "New York";
anArray[5] = "Chicago";

Fall into an array like this : 陷入这样的数组:

anArray[0] = "New York";
anArray[1] = "New York";
anArray[2] = "New York";
anArray[3] = "London";
anArray[4] = "London";
anArray[5] = "Chicago";

Where elements are sorted by amount of equal elements are there in the array. 元素按数组中相等元素的数量排序。 And if for example anArray has an equal amount of elements like : 例如,如果anArray具有相等数量的元素,例如:

anArray[0] = "New York";
anArray[1] = "New York";
anArray[2] = "London";
anArray[3] = "London";

How do i make so that a program finds both of these elements and outputs something like: New York, amount of elements 2 London, amount of elements 2 我如何做才能使程序找到这两个元素并输出如下内容:纽约,元素数量2伦敦,元素数量2

I'll hope you understand, sorry if it may be confusing, thanks for any help here. 希望您能理解,如果可能造成混淆,对不起,谢谢您的帮助。

Maybe not the fastest solution, but this is the logic you are after. 也许不是最快的解决方案,但这是您所追求的逻辑。

var result = anArray.OrderByDescending(o => anArray.Count(c => c == o));

note that you are using an array of size 50, so the first 44 items are going to be null , due to the orderby. 请注意,您使用的数组大小为50,因此由于orderby,前44个项目将为null

You can order by the count of each element 您可以按每个元素的数量排序

var orderedItems = anArray.OrderByDescending(x => x?.Count()).ToArray();

Or you could do it by the length of each string 或者您可以按每个字符串的长度来执行

var orderedItems = anArray.OrderByDescending(x => x?.Length).ToArray();

Both check and return null if the array element is null and returns an ordered array. 如果array元素为null,则检查并返回null,并返回有序数组。

Just use Linq GroupBy 只需使用Linq GroupBy

        string[] anArray = new string[50];
        anArray[0] = "New York";
        anArray[1] = "London";
        anArray[2] = "New York";
        anArray[3] = "London";
        anArray[4] = "New York";
        anArray[5] = "Chicago";

        var GroupedArray = anArray.GroupBy(a => a);
        foreach (var item in GroupedArray)
        {
            Console.WriteLine(item.Key + " -> " + item.Count());
        }
        Console.Read();

        //NewYork -> 3
        //London -> 2
        //Chicago -> 1

On a side note, you initialized the array to hold 50 items which in my example will result in 44 empty string items being taken into account by the groupby clause. 附带说明一下,您将数组初始化为容纳50个项目,在我的示例中,groupby子句将考虑到44个空字符串项目。 If you want to avoid displaying these, replace the groupby line for this one: 如果要避免显示这些内容,请替换此行的groupby行:

var GroupedArray = anArray.GroupBy(a => a).Where(a => !string.IsNullOrEmpty(a.Key));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM