[英]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.