Click here to Skip to main content
Click here to Skip to main content

Numeric before alpha sort in LINQ

By , 11 Feb 2013
Rate this:
Please Sign up or sign in to vote.

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

About the Author

ThunderDK
Software Developer
Denmark Denmark
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberKristian Koch11-Feb-13 10:33 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 11 Feb 2013
Article Copyright 2013 by ThunderDK
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid