Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Numeric before alpha sort in LINQ

0.00/5 (No votes)
11 Feb 2013 1  
Shows how to sort strings in LINQ ordering numbers as first word in order

Introduction

This will help sort a list of strings that has the numbers as the first word in numeric order.

Using the code

This is a code example, output and full compare class that shows how to use the string compare.

// Example
IList<string> cMovies = new List<string>();
cMovies.Add("2 Fast 2 Furious");
cMovies.Add("007 Casino Royale");
cMovies.Add("8 mm");
cMovies.Add("21 jump Street");
cMovies.Add("3:10 To Yuma");
cMovies.Add("40 Days And 40 Nights");
cMovies.Add("1408");
cMovies.Add("2012");
cMovies.Add("10.000 BC");

foreach (string cTitle in cMovies.OrderBy(t => t)) {
    Console.WriteLine(cTitle);
}
Console.WriteLine("-----");
foreach (string cTitle in cMovies.OrderBy(t => t, new NumeralAlphaCompare())) {
    Console.WriteLine(cTitle);
}
// Output
007 Casino Royale
10.000 BC
1408
2 Fast 2 Furious
2012
21 jump Street
3:10 To Yuma
40 Days And 40 Nights
8 mm
-----
2 Fast 2 Furious
3:10 To Yuma
007 Casino Royale
8 mm
21 jump Street
40 Days And 40 Nights
1408
2012
10.000 BC 
// Compare class
using System.Collections.Generic;

public class NumeralAlphaCompare : IComparer<string> {
    public int Compare(string x, string y) {
        int nIndexX = x.Replace(":", " ").IndexOf(" ");
        int nIndexY = y.Replace(":", " ").IndexOf(" ");
        bool bSpaceX = nIndexX > -1;
        bool bSpaceY = nIndexY > -1;

        long nX;
        long nY;
        if (bSpaceX && bSpaceY) {
            if (long.TryParse(x.Substring(0, nIndexX).Replace(".", ""), out nX)
                && long.TryParse(y.Substring(0, nIndexY).Replace(".", ""), out nY)) {
                if (nX < nY) {
                    return -1;
                }
                else if (nX > nY) {
                    return 1;
                }
            }
        }
        else if (bSpaceX) {
            if (long.TryParse(x.Substring(0, nIndexX).Replace(".", ""), out nX)
                && long.TryParse(y, out nY)) {
                if (nX < nY) {
                    return -1;
                }
                else if (nX > nY) {
                    return 1;
                }
            }
        }
        else if (bSpaceY) {
            if (long.TryParse(x, out nX)
                && long.TryParse(y.Substring(0, nIndexY).Replace(".", ""), out nY)) {
                if (nX < nY) {
                    return -1;
                }
                else if (nX > nY) {
                    return 1;
                }
            }
        }
        else {
            if (long.TryParse(x, out nX)
                && long.TryParse(y, out nY)) {
                if (nX < nY) {
                    return -1;
                }
                else if (nX > nY) {
                    return 1;
                }
            }
        }
        return x.CompareTo(y);
    }
}
I know there isn't much text to explain what is happening. But the code is really a bunch of if and else that compares two strings to see which has a number as first word and returns the compare result. 

Points of Interest

I hope you can use this little example to order strings as you wanted it to be sorted like windows sorts files in explorers.

History

No history since this is the first version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here