简体   繁体   English

排序字符串列表的最快方法

[英]fastest way to sort list of strings

I have list of strings like this 我有这样的字符串列表

"FirstName-Lastname" (separated by a dash -) “名字-姓氏”(用破折号-分隔)

I need to sort it this list by first name and then by last name 我需要按照名字和姓氏对列表进行排序

What is the best way to do this. 做这个的最好方式是什么。 i need to process 30000 strings in less than 1 second. 我需要在不到1秒的时间内处理30000个字符串。

I was suggested to use LINQ to Entities. 建议我对实体使用LINQ。

what is the best way to use this? 最好的方法是什么?

thanks a lot for any help 非常感谢您的帮助

EDIT: to avoid confusion the input is List<string> 编辑:为避免混淆,输入为List<string>

您可以使用LINQ(这将允许您排序而无需修改列表中的数据):

names.OrderBy(s => s.Split('-')[0]).ThenBy(s => s.Split('-')[1]);

You could also use delegates 您也可以使用委托

names.Sort(
    delegate(string str1, string str2)
    {
        return str1.CompareTo(str2);
    }
);

Split your array of strings in 26 subarrays, one for each letter of the alphabet. 将您的字符串数组拆分为26个子数组,每个字母对应一个。 Then repeat the process for each subarray. 然后为每个子数组重复该过程。

This method is very effective for sorting strings. 此方法对字符串排序非常有效。 It's known as bucket sort . 这就是所谓的存储桶排序

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

相关问题 在int列表中插入值然后对列表进行排序的最快方法 - Fastest way to insert a value in int List and then sort the list 列出Sort IComparer是检查集合是否更改的最快方法 - List Sort IComparer whats the fastest way to check if collection changed 通过两个字段对字典进行排序的最快方法,一个按字母排序,另一个是自定义字段? - Fastest way to sort a List by two fields, one alphabetical and the other custom? 将字符串列表转换为单个连接字符串的最快方法? - Fastest way to convert a list of strings into a single concatenated string? 从列表中选择所有字符串的最快方法 - Fastest way to select all strings from list starting from 从循环中创建唯一字符串列表的最快方法? - Fastest way to create a list of unique strings from within a loop? 查找文件中字符串的最快方法 - Fastest way to find strings in a file 在制作智能感知/自动完成列表时,过滤字符串列表的最快方法是什么? - What is the fastest way to filter a list of strings when making an Intellisense/Autocomplete list? 有没有办法通过在 c# 中结束 substring 来对字符串列表进行排序? - Is there a way to sort a list of strings by ending substring in c#? 排序字符串列表(housenumbers) - sort a list of strings(housenumbers)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM