Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Tip/Trick

Numeric before alpha sort in LINQ

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
11 Feb 2013CPOL 15.7K   2   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.

C#
// 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 
C#
// 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Kristian Koch11-Feb-13 10:33
Kristian Koch11-Feb-13 10:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.