Posts

Showing posts with the label name

Order strings by name, handling numbers naturally

Here's how to sort a collection of strings, such as file names, with numbers being handled naturally. The example is in C#.NET, but the concept is portable. private static readonly int INT_MAX_DIGITS = Int32.MaxValue.ToString().Length; public static string ConvertForNaturalOrdering( string path) { var sb = new StringBuilder(); var digitCache = new StringBuilder(); foreach ( char ch in path) { if (Char.IsDigit(ch)) { digitCache.Append(ch); continue ; } if (digitCache.Length > 0) { sb.Append(digitCache.ToString().PadLeft(INT_MAX_DIGITS, '0' )); digitCache.Length = 0; } sb.Append(ch); } if (digitCache.Length > 0) sb.Append(digitCache.ToString().PadLeft(INT_MAX_DIGITS, '0' )); return sb.ToString(); } Here is how the method can be used. var sorted = from path in new [] { "aab2" , "aaa10" , "aaa2" , "aaa1 1" } orderby ConvertForNaturalOrdering(path) select pat...

Random name generator

This is a random name generator written in Javascript. It includes a swear filter. I wrote the source code myself and have made it public domain. That is, anyone can use the code for any reason and edit it however they like. Generated name:   You can download the source code here: Random Name Generator.7z This code is completely free to use by anybody. It is held under the Do What You Want To Public License: http://tinyurl.com/DWYWTPL