Sort List like Excel columns sort










3















Please consider this List:



"A", "C", "AB", "AD", "N", "Z", "AC"


I want to sort this string (That are being Excel column) like Excel Column Sorting.



I want the result like this:



"A", "C", "N", "Z", "AB", "AC", "AD"


Is it possible using LINQ OrderBy?



What is the best approach?



Thanks










share|improve this question






















  • If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.

    – Mohit Shrivastava
    Dec 28 '15 at 7:05






  • 3





    Just to note: This order is called quasi-lexicographic order.

    – Joey
    Dec 28 '15 at 7:10











  • If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want to Sort the List<> in-place, you could use something like yourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));. If the list may contain null values, you need to take care of that in the lambda of course.

    – Jeppe Stig Nielsen
    Dec 28 '15 at 7:30











  • @MohitShrivastava But that would make "AB" and "BA" equal.

    – Jeppe Stig Nielsen
    Dec 28 '15 at 7:33











  • Absolutely Right @JeppeStigNielsen. .OrderBy(p=>p.Length).ThenBy(p=>p); is the best way to do so. :)

    – Mohit Shrivastava
    Dec 28 '15 at 7:34















3















Please consider this List:



"A", "C", "AB", "AD", "N", "Z", "AC"


I want to sort this string (That are being Excel column) like Excel Column Sorting.



I want the result like this:



"A", "C", "N", "Z", "AB", "AC", "AD"


Is it possible using LINQ OrderBy?



What is the best approach?



Thanks










share|improve this question






















  • If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.

    – Mohit Shrivastava
    Dec 28 '15 at 7:05






  • 3





    Just to note: This order is called quasi-lexicographic order.

    – Joey
    Dec 28 '15 at 7:10











  • If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want to Sort the List<> in-place, you could use something like yourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));. If the list may contain null values, you need to take care of that in the lambda of course.

    – Jeppe Stig Nielsen
    Dec 28 '15 at 7:30











  • @MohitShrivastava But that would make "AB" and "BA" equal.

    – Jeppe Stig Nielsen
    Dec 28 '15 at 7:33











  • Absolutely Right @JeppeStigNielsen. .OrderBy(p=>p.Length).ThenBy(p=>p); is the best way to do so. :)

    – Mohit Shrivastava
    Dec 28 '15 at 7:34













3












3








3








Please consider this List:



"A", "C", "AB", "AD", "N", "Z", "AC"


I want to sort this string (That are being Excel column) like Excel Column Sorting.



I want the result like this:



"A", "C", "N", "Z", "AB", "AC", "AD"


Is it possible using LINQ OrderBy?



What is the best approach?



Thanks










share|improve this question














Please consider this List:



"A", "C", "AB", "AD", "N", "Z", "AC"


I want to sort this string (That are being Excel column) like Excel Column Sorting.



I want the result like this:



"A", "C", "N", "Z", "AB", "AC", "AD"


Is it possible using LINQ OrderBy?



What is the best approach?



Thanks







c# linq sorting c#-4.0






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '15 at 6:58









ArianArian

4,89150150248




4,89150150248












  • If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.

    – Mohit Shrivastava
    Dec 28 '15 at 7:05






  • 3





    Just to note: This order is called quasi-lexicographic order.

    – Joey
    Dec 28 '15 at 7:10











  • If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want to Sort the List<> in-place, you could use something like yourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));. If the list may contain null values, you need to take care of that in the lambda of course.

    – Jeppe Stig Nielsen
    Dec 28 '15 at 7:30











  • @MohitShrivastava But that would make "AB" and "BA" equal.

    – Jeppe Stig Nielsen
    Dec 28 '15 at 7:33











  • Absolutely Right @JeppeStigNielsen. .OrderBy(p=>p.Length).ThenBy(p=>p); is the best way to do so. :)

    – Mohit Shrivastava
    Dec 28 '15 at 7:34

















  • If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.

    – Mohit Shrivastava
    Dec 28 '15 at 7:05






  • 3





    Just to note: This order is called quasi-lexicographic order.

    – Joey
    Dec 28 '15 at 7:10











  • If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want to Sort the List<> in-place, you could use something like yourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));. If the list may contain null values, you need to take care of that in the lambda of course.

    – Jeppe Stig Nielsen
    Dec 28 '15 at 7:30











  • @MohitShrivastava But that would make "AB" and "BA" equal.

    – Jeppe Stig Nielsen
    Dec 28 '15 at 7:33











  • Absolutely Right @JeppeStigNielsen. .OrderBy(p=>p.Length).ThenBy(p=>p); is the best way to do so. :)

    – Mohit Shrivastava
    Dec 28 '15 at 7:34
















If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.

– Mohit Shrivastava
Dec 28 '15 at 7:05





If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.

– Mohit Shrivastava
Dec 28 '15 at 7:05




3




3





Just to note: This order is called quasi-lexicographic order.

– Joey
Dec 28 '15 at 7:10





Just to note: This order is called quasi-lexicographic order.

– Joey
Dec 28 '15 at 7:10













If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want to Sort the List<> in-place, you could use something like yourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));. If the list may contain null values, you need to take care of that in the lambda of course.

– Jeppe Stig Nielsen
Dec 28 '15 at 7:30





If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want to Sort the List<> in-place, you could use something like yourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));. If the list may contain null values, you need to take care of that in the lambda of course.

– Jeppe Stig Nielsen
Dec 28 '15 at 7:30













@MohitShrivastava But that would make "AB" and "BA" equal.

– Jeppe Stig Nielsen
Dec 28 '15 at 7:33





@MohitShrivastava But that would make "AB" and "BA" equal.

– Jeppe Stig Nielsen
Dec 28 '15 at 7:33













Absolutely Right @JeppeStigNielsen. .OrderBy(p=>p.Length).ThenBy(p=>p); is the best way to do so. :)

– Mohit Shrivastava
Dec 28 '15 at 7:34





Absolutely Right @JeppeStigNielsen. .OrderBy(p=>p.Length).ThenBy(p=>p); is the best way to do so. :)

– Mohit Shrivastava
Dec 28 '15 at 7:34












2 Answers
2






active

oldest

votes


















11














Update: Thanks @JeppeStigNielsen for correct comment, I add StringComparer.Ordinal to support in all cultures:



var result = List.OrderBy(p=>p.Length).ThenBy(p=>p,StringComparer.Ordinal);





share|improve this answer




















  • 2





    I thought a bit about it. That should be .ThenBy(p=>p, StringComparer.Ordinal) in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania) "lt-LT" you would have "Y" before "R" (the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic) "cs-CZ" we get "DI" before "CH" since CH is a digraph in Czech and comes after H in its collation. Test for yourself with StringComparer.Create(new CultureInfo("lt-LT"), false) etc.

    – Jeppe Stig Nielsen
    Dec 28 '15 at 8:54


















3














First order by the length of column name ("C" comes before "AB"), then use normal alphabetical (string) sorting on strings with same length ("AC" before "AD").



var columns = new "A", "C", "AB", "AD", "N", "Z", "AC" ;
var sorted = columns.OrderBy(c => c.Length)
.ThenBy(c => c);





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34488997%2fsort-liststring-like-excel-columns-sort%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    11














    Update: Thanks @JeppeStigNielsen for correct comment, I add StringComparer.Ordinal to support in all cultures:



    var result = List.OrderBy(p=>p.Length).ThenBy(p=>p,StringComparer.Ordinal);





    share|improve this answer




















    • 2





      I thought a bit about it. That should be .ThenBy(p=>p, StringComparer.Ordinal) in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania) "lt-LT" you would have "Y" before "R" (the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic) "cs-CZ" we get "DI" before "CH" since CH is a digraph in Czech and comes after H in its collation. Test for yourself with StringComparer.Create(new CultureInfo("lt-LT"), false) etc.

      – Jeppe Stig Nielsen
      Dec 28 '15 at 8:54















    11














    Update: Thanks @JeppeStigNielsen for correct comment, I add StringComparer.Ordinal to support in all cultures:



    var result = List.OrderBy(p=>p.Length).ThenBy(p=>p,StringComparer.Ordinal);





    share|improve this answer




















    • 2





      I thought a bit about it. That should be .ThenBy(p=>p, StringComparer.Ordinal) in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania) "lt-LT" you would have "Y" before "R" (the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic) "cs-CZ" we get "DI" before "CH" since CH is a digraph in Czech and comes after H in its collation. Test for yourself with StringComparer.Create(new CultureInfo("lt-LT"), false) etc.

      – Jeppe Stig Nielsen
      Dec 28 '15 at 8:54













    11












    11








    11







    Update: Thanks @JeppeStigNielsen for correct comment, I add StringComparer.Ordinal to support in all cultures:



    var result = List.OrderBy(p=>p.Length).ThenBy(p=>p,StringComparer.Ordinal);





    share|improve this answer















    Update: Thanks @JeppeStigNielsen for correct comment, I add StringComparer.Ordinal to support in all cultures:



    var result = List.OrderBy(p=>p.Length).ThenBy(p=>p,StringComparer.Ordinal);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 28 '15 at 10:57

























    answered Dec 28 '15 at 7:00









    Reza ArabQaeniReza ArabQaeni

    4,1942038




    4,1942038







    • 2





      I thought a bit about it. That should be .ThenBy(p=>p, StringComparer.Ordinal) in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania) "lt-LT" you would have "Y" before "R" (the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic) "cs-CZ" we get "DI" before "CH" since CH is a digraph in Czech and comes after H in its collation. Test for yourself with StringComparer.Create(new CultureInfo("lt-LT"), false) etc.

      – Jeppe Stig Nielsen
      Dec 28 '15 at 8:54












    • 2





      I thought a bit about it. That should be .ThenBy(p=>p, StringComparer.Ordinal) in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania) "lt-LT" you would have "Y" before "R" (the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic) "cs-CZ" we get "DI" before "CH" since CH is a digraph in Czech and comes after H in its collation. Test for yourself with StringComparer.Create(new CultureInfo("lt-LT"), false) etc.

      – Jeppe Stig Nielsen
      Dec 28 '15 at 8:54







    2




    2





    I thought a bit about it. That should be .ThenBy(p=>p, StringComparer.Ordinal) in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania) "lt-LT" you would have "Y" before "R" (the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic) "cs-CZ" we get "DI" before "CH" since CH is a digraph in Czech and comes after H in its collation. Test for yourself with StringComparer.Create(new CultureInfo("lt-LT"), false) etc.

    – Jeppe Stig Nielsen
    Dec 28 '15 at 8:54





    I thought a bit about it. That should be .ThenBy(p=>p, StringComparer.Ordinal) in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania) "lt-LT" you would have "Y" before "R" (the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic) "cs-CZ" we get "DI" before "CH" since CH is a digraph in Czech and comes after H in its collation. Test for yourself with StringComparer.Create(new CultureInfo("lt-LT"), false) etc.

    – Jeppe Stig Nielsen
    Dec 28 '15 at 8:54













    3














    First order by the length of column name ("C" comes before "AB"), then use normal alphabetical (string) sorting on strings with same length ("AC" before "AD").



    var columns = new "A", "C", "AB", "AD", "N", "Z", "AC" ;
    var sorted = columns.OrderBy(c => c.Length)
    .ThenBy(c => c);





    share|improve this answer



























      3














      First order by the length of column name ("C" comes before "AB"), then use normal alphabetical (string) sorting on strings with same length ("AC" before "AD").



      var columns = new "A", "C", "AB", "AD", "N", "Z", "AC" ;
      var sorted = columns.OrderBy(c => c.Length)
      .ThenBy(c => c);





      share|improve this answer

























        3












        3








        3







        First order by the length of column name ("C" comes before "AB"), then use normal alphabetical (string) sorting on strings with same length ("AC" before "AD").



        var columns = new "A", "C", "AB", "AD", "N", "Z", "AC" ;
        var sorted = columns.OrderBy(c => c.Length)
        .ThenBy(c => c);





        share|improve this answer













        First order by the length of column name ("C" comes before "AB"), then use normal alphabetical (string) sorting on strings with same length ("AC" before "AD").



        var columns = new "A", "C", "AB", "AD", "N", "Z", "AC" ;
        var sorted = columns.OrderBy(c => c.Length)
        .ThenBy(c => c);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 28 '15 at 7:01









        Arghya CArghya C

        6,24012342




        6,24012342



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34488997%2fsort-liststring-like-excel-columns-sort%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党