How to sort an array of string alphabetically (case sensitive, nonstandard collation)

Keep alike words together… For lists of words, it is often more useful to group the “same” words together (even though they differ in case). For example: Keeping things together: Simple “M after m”: ———————— ——————- mars mars mars bar mars bar Mars bar milk milk milk-duds Milk milky-way milk-duds Mars bar milky-way Milk Milky-way … Read more

Simple way to sort strings in the (case sensitive) alphabetical order

If you don’t want to add a dependency on Guava (per Michael’s answer) then this comparator is equivalent: private static Comparator<String> ALPHABETICAL_ORDER = new Comparator<String>() { public int compare(String str1, String str2) { int res = String.CASE_INSENSITIVE_ORDER.compare(str1, str2); if (res == 0) { res = str1.compareTo(str2); } return res; } }; Collections.sort(list, ALPHABETICAL_ORDER); And I … Read more

How to make a continuous alphabetic list python (from a-z then from aa, ab, ac etc)

Use itertools.product. from string import ascii_lowercase import itertools def iter_all_strings(): for size in itertools.count(1): for s in itertools.product(ascii_lowercase, repeat=size): yield “”.join(s) for s in iter_all_strings(): print(s) if s == ‘bb’: break Result: a b c d e … y z aa ab ac … ay az ba bb This has the added benefit of going … Read more

How do I sort strings alphabetically while accounting for value when a string is numeric?

Pass a custom comparer into OrderBy. Enumerable.OrderBy will let you specify any comparer you like. This is one way to do that: void Main() { string[] things = new string[] { “paul”, “bob”, “lauren”, “007”, “90”, “101”}; foreach (var thing in things.OrderBy(x => x, new SemiNumericComparer())) { Console.WriteLine(thing); } } public class SemiNumericComparer: IComparer<string> { … Read more