Dynamically sort a list by array of items [duplicate]












-2
















This question already has an answer here:




  • Sort a list by a custom order

    3 answers




I have a list which I am currently sorting manually by using .OrderBy:



var newList = pouchList
.OrderBy(x => x.ID3Val)
.ThenBy(x => x.ID4Val == "LATE")
.ThenBy(x => x.ID4Val == "BED")
.ThenBy(x => x.ID4Val == "TEA")
.ThenBy(x => x.ID4Val == "LNCH")
.ThenBy(x => x.ID4Val == "MORN")
.ToList();


What I would like to do is have the parameters to which the list is ordered by in an array ie:



[ "LATE", "BED", "TEA", "LNCH", "MORN" ]


so that I can change the array and have the list sorted dynamically using these parameters.



Does anyone know if this is possible?










share|improve this question















marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 22:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    It would be awesome if you could provide a Minimal, Complete, and Verifiable example with sample inputs and expected results based on those sample inputs. This makes it easier for us to help you, since we don't need to waste time guesses what the classes and data look like.

    – mjwills
    Nov 13 '18 at 22:21











  • The parameter to OrderBy (and to ThenBy) is a KeySelector. You use it to select which column you want to sort by. All of your ThenBy methods are given a lambda expression that evaluates to a Boolean. What you want to do is add an IComparer implementation that does your comparisons and figures out your sort order (docs.microsoft.com/en-us/dotnet/api/…). If you augment your question with enough information (in particular, the contents of the array you are starting with), I'll provide an answer.

    – Flydog57
    Nov 13 '18 at 22:27













  • By the way, as this was getting marked as a dup, I prepared an answer for you. The answer involved creating a custom IComparer<string> that used an enum to establish sort order. The enum looked like public enum PouchOrder { LATE, BED, TEA, LNCH, MORN, BAD, }. Anything that didn't parse as an enum of that type (using enum.TryParse got marked as "BAD" and sorted last.

    – Flydog57
    Nov 13 '18 at 22:56
















-2
















This question already has an answer here:




  • Sort a list by a custom order

    3 answers




I have a list which I am currently sorting manually by using .OrderBy:



var newList = pouchList
.OrderBy(x => x.ID3Val)
.ThenBy(x => x.ID4Val == "LATE")
.ThenBy(x => x.ID4Val == "BED")
.ThenBy(x => x.ID4Val == "TEA")
.ThenBy(x => x.ID4Val == "LNCH")
.ThenBy(x => x.ID4Val == "MORN")
.ToList();


What I would like to do is have the parameters to which the list is ordered by in an array ie:



[ "LATE", "BED", "TEA", "LNCH", "MORN" ]


so that I can change the array and have the list sorted dynamically using these parameters.



Does anyone know if this is possible?










share|improve this question















marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 22:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    It would be awesome if you could provide a Minimal, Complete, and Verifiable example with sample inputs and expected results based on those sample inputs. This makes it easier for us to help you, since we don't need to waste time guesses what the classes and data look like.

    – mjwills
    Nov 13 '18 at 22:21











  • The parameter to OrderBy (and to ThenBy) is a KeySelector. You use it to select which column you want to sort by. All of your ThenBy methods are given a lambda expression that evaluates to a Boolean. What you want to do is add an IComparer implementation that does your comparisons and figures out your sort order (docs.microsoft.com/en-us/dotnet/api/…). If you augment your question with enough information (in particular, the contents of the array you are starting with), I'll provide an answer.

    – Flydog57
    Nov 13 '18 at 22:27













  • By the way, as this was getting marked as a dup, I prepared an answer for you. The answer involved creating a custom IComparer<string> that used an enum to establish sort order. The enum looked like public enum PouchOrder { LATE, BED, TEA, LNCH, MORN, BAD, }. Anything that didn't parse as an enum of that type (using enum.TryParse got marked as "BAD" and sorted last.

    – Flydog57
    Nov 13 '18 at 22:56














-2












-2








-2









This question already has an answer here:




  • Sort a list by a custom order

    3 answers




I have a list which I am currently sorting manually by using .OrderBy:



var newList = pouchList
.OrderBy(x => x.ID3Val)
.ThenBy(x => x.ID4Val == "LATE")
.ThenBy(x => x.ID4Val == "BED")
.ThenBy(x => x.ID4Val == "TEA")
.ThenBy(x => x.ID4Val == "LNCH")
.ThenBy(x => x.ID4Val == "MORN")
.ToList();


What I would like to do is have the parameters to which the list is ordered by in an array ie:



[ "LATE", "BED", "TEA", "LNCH", "MORN" ]


so that I can change the array and have the list sorted dynamically using these parameters.



Does anyone know if this is possible?










share|improve this question

















This question already has an answer here:




  • Sort a list by a custom order

    3 answers




I have a list which I am currently sorting manually by using .OrderBy:



var newList = pouchList
.OrderBy(x => x.ID3Val)
.ThenBy(x => x.ID4Val == "LATE")
.ThenBy(x => x.ID4Val == "BED")
.ThenBy(x => x.ID4Val == "TEA")
.ThenBy(x => x.ID4Val == "LNCH")
.ThenBy(x => x.ID4Val == "MORN")
.ToList();


What I would like to do is have the parameters to which the list is ordered by in an array ie:



[ "LATE", "BED", "TEA", "LNCH", "MORN" ]


so that I can change the array and have the list sorted dynamically using these parameters.



Does anyone know if this is possible?





This question already has an answer here:




  • Sort a list by a custom order

    3 answers








c# list linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 22:25









RagtimeWilly

3,44321333




3,44321333










asked Nov 13 '18 at 22:20









Brownd92Brownd92

357




357




marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 22:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 22:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    It would be awesome if you could provide a Minimal, Complete, and Verifiable example with sample inputs and expected results based on those sample inputs. This makes it easier for us to help you, since we don't need to waste time guesses what the classes and data look like.

    – mjwills
    Nov 13 '18 at 22:21











  • The parameter to OrderBy (and to ThenBy) is a KeySelector. You use it to select which column you want to sort by. All of your ThenBy methods are given a lambda expression that evaluates to a Boolean. What you want to do is add an IComparer implementation that does your comparisons and figures out your sort order (docs.microsoft.com/en-us/dotnet/api/…). If you augment your question with enough information (in particular, the contents of the array you are starting with), I'll provide an answer.

    – Flydog57
    Nov 13 '18 at 22:27













  • By the way, as this was getting marked as a dup, I prepared an answer for you. The answer involved creating a custom IComparer<string> that used an enum to establish sort order. The enum looked like public enum PouchOrder { LATE, BED, TEA, LNCH, MORN, BAD, }. Anything that didn't parse as an enum of that type (using enum.TryParse got marked as "BAD" and sorted last.

    – Flydog57
    Nov 13 '18 at 22:56














  • 2





    It would be awesome if you could provide a Minimal, Complete, and Verifiable example with sample inputs and expected results based on those sample inputs. This makes it easier for us to help you, since we don't need to waste time guesses what the classes and data look like.

    – mjwills
    Nov 13 '18 at 22:21











  • The parameter to OrderBy (and to ThenBy) is a KeySelector. You use it to select which column you want to sort by. All of your ThenBy methods are given a lambda expression that evaluates to a Boolean. What you want to do is add an IComparer implementation that does your comparisons and figures out your sort order (docs.microsoft.com/en-us/dotnet/api/…). If you augment your question with enough information (in particular, the contents of the array you are starting with), I'll provide an answer.

    – Flydog57
    Nov 13 '18 at 22:27













  • By the way, as this was getting marked as a dup, I prepared an answer for you. The answer involved creating a custom IComparer<string> that used an enum to establish sort order. The enum looked like public enum PouchOrder { LATE, BED, TEA, LNCH, MORN, BAD, }. Anything that didn't parse as an enum of that type (using enum.TryParse got marked as "BAD" and sorted last.

    – Flydog57
    Nov 13 '18 at 22:56








2




2





It would be awesome if you could provide a Minimal, Complete, and Verifiable example with sample inputs and expected results based on those sample inputs. This makes it easier for us to help you, since we don't need to waste time guesses what the classes and data look like.

– mjwills
Nov 13 '18 at 22:21





It would be awesome if you could provide a Minimal, Complete, and Verifiable example with sample inputs and expected results based on those sample inputs. This makes it easier for us to help you, since we don't need to waste time guesses what the classes and data look like.

– mjwills
Nov 13 '18 at 22:21













The parameter to OrderBy (and to ThenBy) is a KeySelector. You use it to select which column you want to sort by. All of your ThenBy methods are given a lambda expression that evaluates to a Boolean. What you want to do is add an IComparer implementation that does your comparisons and figures out your sort order (docs.microsoft.com/en-us/dotnet/api/…). If you augment your question with enough information (in particular, the contents of the array you are starting with), I'll provide an answer.

– Flydog57
Nov 13 '18 at 22:27







The parameter to OrderBy (and to ThenBy) is a KeySelector. You use it to select which column you want to sort by. All of your ThenBy methods are given a lambda expression that evaluates to a Boolean. What you want to do is add an IComparer implementation that does your comparisons and figures out your sort order (docs.microsoft.com/en-us/dotnet/api/…). If you augment your question with enough information (in particular, the contents of the array you are starting with), I'll provide an answer.

– Flydog57
Nov 13 '18 at 22:27















By the way, as this was getting marked as a dup, I prepared an answer for you. The answer involved creating a custom IComparer<string> that used an enum to establish sort order. The enum looked like public enum PouchOrder { LATE, BED, TEA, LNCH, MORN, BAD, }. Anything that didn't parse as an enum of that type (using enum.TryParse got marked as "BAD" and sorted last.

– Flydog57
Nov 13 '18 at 22:56





By the way, as this was getting marked as a dup, I prepared an answer for you. The answer involved creating a custom IComparer<string> that used an enum to establish sort order. The enum looked like public enum PouchOrder { LATE, BED, TEA, LNCH, MORN, BAD, }. Anything that didn't parse as an enum of that type (using enum.TryParse got marked as "BAD" and sorted last.

– Flydog57
Nov 13 '18 at 22:56












1 Answer
1






active

oldest

votes


















0














Each of the .OrderBy() and .ThenBy() calls will return a IOrderedEnumerable object. You can store the result of OrderBy() in a variable, then iterate over your array of values and replace that variable with a result of each ThenBy() call. Something like that:



var orderingList = new string {"LATE", "BED", "TEA", "LNCH", "MORN"};
var newEnumerable = pouchList.OrderBy(x => x.ID3Val);
for(int i = 0; i < orderingList.Length; i++)
{
newList = newList.ThenBy(x => x.ID4Val == orderingList[i]);
}
var newList = newEnumerable.ToList();


Here's a MSDN page for reference: ThenBy






share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Each of the .OrderBy() and .ThenBy() calls will return a IOrderedEnumerable object. You can store the result of OrderBy() in a variable, then iterate over your array of values and replace that variable with a result of each ThenBy() call. Something like that:



    var orderingList = new string {"LATE", "BED", "TEA", "LNCH", "MORN"};
    var newEnumerable = pouchList.OrderBy(x => x.ID3Val);
    for(int i = 0; i < orderingList.Length; i++)
    {
    newList = newList.ThenBy(x => x.ID4Val == orderingList[i]);
    }
    var newList = newEnumerable.ToList();


    Here's a MSDN page for reference: ThenBy






    share|improve this answer






























      0














      Each of the .OrderBy() and .ThenBy() calls will return a IOrderedEnumerable object. You can store the result of OrderBy() in a variable, then iterate over your array of values and replace that variable with a result of each ThenBy() call. Something like that:



      var orderingList = new string {"LATE", "BED", "TEA", "LNCH", "MORN"};
      var newEnumerable = pouchList.OrderBy(x => x.ID3Val);
      for(int i = 0; i < orderingList.Length; i++)
      {
      newList = newList.ThenBy(x => x.ID4Val == orderingList[i]);
      }
      var newList = newEnumerable.ToList();


      Here's a MSDN page for reference: ThenBy






      share|improve this answer




























        0












        0








        0







        Each of the .OrderBy() and .ThenBy() calls will return a IOrderedEnumerable object. You can store the result of OrderBy() in a variable, then iterate over your array of values and replace that variable with a result of each ThenBy() call. Something like that:



        var orderingList = new string {"LATE", "BED", "TEA", "LNCH", "MORN"};
        var newEnumerable = pouchList.OrderBy(x => x.ID3Val);
        for(int i = 0; i < orderingList.Length; i++)
        {
        newList = newList.ThenBy(x => x.ID4Val == orderingList[i]);
        }
        var newList = newEnumerable.ToList();


        Here's a MSDN page for reference: ThenBy






        share|improve this answer















        Each of the .OrderBy() and .ThenBy() calls will return a IOrderedEnumerable object. You can store the result of OrderBy() in a variable, then iterate over your array of values and replace that variable with a result of each ThenBy() call. Something like that:



        var orderingList = new string {"LATE", "BED", "TEA", "LNCH", "MORN"};
        var newEnumerable = pouchList.OrderBy(x => x.ID3Val);
        for(int i = 0; i < orderingList.Length; i++)
        {
        newList = newList.ThenBy(x => x.ID4Val == orderingList[i]);
        }
        var newList = newEnumerable.ToList();


        Here's a MSDN page for reference: ThenBy







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 22:36

























        answered Nov 13 '18 at 22:30









        SzabSzab

        1,053517




        1,053517















            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues