1

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.

I was suggested to use LINQ to Entities.

what is the best way to use this?

thanks a lot for any help

EDIT: to avoid confusion the input is List<string>

6
  • L2E? Is the data in a database or in memory? Apr 3, 2012 at 16:12
  • Where does the data come from?
    – mellamokb
    Apr 3, 2012 at 16:12
  • 2
    please show som source code... what have your tried ?
    – Yahia
    Apr 3, 2012 at 16:12
  • How long does it take to execute using LINQ to Entities?
    – Slugart
    Apr 3, 2012 at 16:12
  • 2
    Try some different sorting strategies and profile the results.
    – Sam Axe
    Apr 3, 2012 at 16:12

3 Answers 3

3

You could use LINQ (which would allow you to sort without modifying the data in the List):

names.OrderBy(s => s.Split('-')[0]).ThenBy(s => s.Split('-')[1]);
10
  • @NiklasB. - Depends on the desired behavior if first names overlap. Consider 'Sam-Jones' and 'Samantha-Smith'. Simply calling OrderBy without splitting the values would put Samantha ahead of Sam but I would expect Sam to come first. Apr 3, 2012 at 16:20
  • I think you meant (Last then First name): names.OrderBy(s => s.Split('-')[1]).ThenBy(s => s.Split('-')[0]); Apr 3, 2012 at 16:20
  • @anderson: No, it's first name, then last name, according to the question.
    – Niklas B.
    Apr 3, 2012 at 16:22
  • Thanks for your answer. The string might not contain a '-' so the query will crash. can you tell me how to handle that?
    – Y2theZ
    Apr 3, 2012 at 16:22
  • @Youssef - In that case, there's no good way to do what you need. You could try just calling Sort() on the list, but that is going to give you strange results if names overlap. Apr 3, 2012 at 16:24
2

You could also use delegates

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

Split your array of strings in 26 subarrays, one for each letter of the alphabet. Then repeat the process for each subarray.

This method is very effective for sorting strings. It's known as bucket sort.

3
  • I would use one of the framework sorting functions and only roll my own sort if there was compelling reasons to believe that a) the program isn't fast enough already and b) the newer sorting function would make more of a difference than some other performance enhancement. There are enough edge cases that the probability of error just isn't worth it considering I'm sure that the quick/merge sort the framework uses is fast enough.
    – Servy
    Apr 3, 2012 at 16:31
  • @Servy: For 30000 strings, it probably doesn't matter. But for really large collections of strings, I found bucket sort to be a huge performance benefit.
    – Dennis
    Apr 3, 2012 at 16:33
  • I don't doubt that it's faster, I'm simply asserting that in this case the benefit isn't worth the risk. If there were a framework solution that used a Radix sort, or if there was compelling evidence that the performance was needed, then it would be used. Since that could be true at some future point (or for other readers) this isn't a wrong answer (hence no -1 from me).
    – Servy
    Apr 3, 2012 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.