Sorting string array in C#
Solution 1
This code snippet is working properly
Solution 2
If you have problems with numbers (say 1, 2, 10, 12 which will be sorted 1, 10, 12, 2) you can use LINQ:
var arr = arr.OrderBy(x=>x).ToArray();
Solution 3
Actually I don't see any nulls:
given:
static void Main()
{
string[] testArray = new string[]
{
"aa",
"ab",
"ac",
"ad",
"ab",
"af"
};
Array.Sort(testArray, StringComparer.InvariantCulture);
Array.ForEach(testArray, x => Console.WriteLine(x));
}
I obtained:
Related videos on Youtube
Peter Mortensen
Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (12 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly, CAN bus/CANopen. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com and possibly other(s)). Make sure to make the subject specific (I said: specific. Repeat: specific subject required). I can not stress this enough - 90% of you can not compose a specific subject, but instead use some generic subject. Use a specific subject, damn it! You still don't get it. It can't be that difficult to provide a specific subject to an email instead of a generic one. For example, including meta content like "quick question" is unhelpful. Concentrate on the actual subject. Did I say specific? I think I did. Let me repeat it just in case: use a specific subject in your email (otherwise it will no be opened at all). Selected questions, etc.: End-of-line identifier in VB.NET? How can I determine if a .NET assembly was built for x86 or x64? C++ reference - sample memmove The difference between + and & for joining strings in VB.NET Some of my other accounts: Careers. [/]. Super User (SU). [/]. Other My 15 minutes of fame on Super User My 15 minutes of fame in Denmark Blog. Sample: Jump the shark. LinkedIn @PeterMortensen (Twitter) Quora GitHub Full jump page (Last updated 2021-11-25)
Updated on July 09, 2022Comments
-
Peter Mortensen almost 2 years
Maybe it sounds weird, but after a long time programming, I just got on array sorting. Everything was as I expected, until I tried to sort an array of strings containing two identical strings inside. Let's see:
Suppose having the following:
string[] testArray = new string[] { "aa", "ab", "ac", "ad", "ab", "af" }; Array.Sort(testArray, StringComparer.InvariantCulture);
In this situation I get an array plain of null values. I got that this behavior is because the values inside array are not distinct values. Is there a better explanation for this? How do I sort a non-distinct array?
-
Mitch Wheat almost 13 years"I got that this behavior is because the values inside array are not distinct values" - unlikely.
-
Anton Semenov almost 13 yearsAre you sure that you are really get null values with code provided? I just test it, and it works correct
-
Henk Holterman almost 13 yearsPlease take some time to verify that the problem does arise from the posted code.
-
Admin almost 13 yearsI humbly apologize! I made a couple of test yesterday and it seemed that the behavior was due to a non distinct values inside the array. I retested everything again and it works properly. Error probably was somewhere else. Thanks for patience! Another question: when a sort method can give as result the array containing null values? Thanks
-
-
Admin almost 13 yearsI humbly apologize! I made a couple of test yesterday and it seemed that the behavior was due to a non distinct values inside the array. I retested everything again and it works properly. Error probably was somewhere else. Thanks for patience! Another question: when a sort method can give as result the array containing null values? Thanks
-
Steve Smith over 3 yearsHow can I copy and paste an image? ;)