Dynamically sort a list by array of items [duplicate]
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?
c# list linq
marked as duplicate by Servy
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.
add a comment |
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?
c# list linq
marked as duplicate by Servy
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 toOrderBy
(and toThenBy
) is aKeySelector
. You use it to select which column you want to sort by. All of yourThenBy
methods are given a lambda expression that evaluates to a Boolean. What you want to do is add anIComparer
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 customIComparer<string>
that used anenum
to establish sort order. The enum looked likepublic enum PouchOrder { LATE, BED, TEA, LNCH, MORN, BAD, }
. Anything that didn't parse as an enum of that type (usingenum.TryParse
got marked as "BAD" and sorted last.
– Flydog57
Nov 13 '18 at 22:56
add a comment |
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?
c# list linq
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
c# list linq
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
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
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 toOrderBy
(and toThenBy
) is aKeySelector
. You use it to select which column you want to sort by. All of yourThenBy
methods are given a lambda expression that evaluates to a Boolean. What you want to do is add anIComparer
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 customIComparer<string>
that used anenum
to establish sort order. The enum looked likepublic enum PouchOrder { LATE, BED, TEA, LNCH, MORN, BAD, }
. Anything that didn't parse as an enum of that type (usingenum.TryParse
got marked as "BAD" and sorted last.
– Flydog57
Nov 13 '18 at 22:56
add a comment |
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 toOrderBy
(and toThenBy
) is aKeySelector
. You use it to select which column you want to sort by. All of yourThenBy
methods are given a lambda expression that evaluates to a Boolean. What you want to do is add anIComparer
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 customIComparer<string>
that used anenum
to establish sort order. The enum looked likepublic enum PouchOrder { LATE, BED, TEA, LNCH, MORN, BAD, }
. Anything that didn't parse as an enum of that type (usingenum.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
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
edited Nov 13 '18 at 22:36
answered Nov 13 '18 at 22:30
SzabSzab
1,053517
1,053517
add a comment |
add a comment |
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 toThenBy
) is aKeySelector
. You use it to select which column you want to sort by. All of yourThenBy
methods are given a lambda expression that evaluates to a Boolean. What you want to do is add anIComparer
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 anenum
to establish sort order. The enum looked likepublic enum PouchOrder { LATE, BED, TEA, LNCH, MORN, BAD, }
. Anything that didn't parse as an enum of that type (usingenum.TryParse
got marked as "BAD" and sorted last.– Flydog57
Nov 13 '18 at 22:56