C# sort string alphabetically followed by frequency of occurrence
I just started learning lambda/linq. Here's what I have so far.
var frequency = from f in "trreill".ToList()
group f by f into letterfrequency
select letterfrequency;
foreach (var f in frequency)
{
Console.WriteLine($"{f.Key}{f.Count()}");
}
Here is the output:
t1 r2 e1 i1 l2
Output needed:
e1i1l2r2t1
Can't figure out how to sort properly. Any thoughts to what I'm doing wrong?
c# linq
|
show 1 more comment
I just started learning lambda/linq. Here's what I have so far.
var frequency = from f in "trreill".ToList()
group f by f into letterfrequency
select letterfrequency;
foreach (var f in frequency)
{
Console.WriteLine($"{f.Key}{f.Count()}");
}
Here is the output:
t1 r2 e1 i1 l2
Output needed:
e1i1l2r2t1
Can't figure out how to sort properly. Any thoughts to what I'm doing wrong?
c# linq
1
frequency = frequency.OrderBy(x => x.Count);
– Ryan Wilson
Nov 20 '18 at 14:58
You simply want to order byletterfrequency.Key
, then byletterfrequency.Count()
? Given your grouping, the latter will not matter.
– CodeCaster
Nov 20 '18 at 14:59
5
Can you explain why you wrote an unnecessaryToList
in there? I am interested to learn why people write unnecessary code because it helps me design APIs that lead developers to avoid writing unnecessary code.
– Eric Lippert
Nov 20 '18 at 15:04
By "followed by" do you mean print out the key and then follow it by the count? Or do you mean that the ordering relation depends on the count? If the latter, how does that ever make a difference?
– Eric Lippert
Nov 20 '18 at 15:08
1
The ToList() was one of the ways I was trying sort, originally, because I couldn't get it to work properly. I know, bad coding but I'm trying:) Many thanks
– MBrewers
Nov 20 '18 at 15:53
|
show 1 more comment
I just started learning lambda/linq. Here's what I have so far.
var frequency = from f in "trreill".ToList()
group f by f into letterfrequency
select letterfrequency;
foreach (var f in frequency)
{
Console.WriteLine($"{f.Key}{f.Count()}");
}
Here is the output:
t1 r2 e1 i1 l2
Output needed:
e1i1l2r2t1
Can't figure out how to sort properly. Any thoughts to what I'm doing wrong?
c# linq
I just started learning lambda/linq. Here's what I have so far.
var frequency = from f in "trreill".ToList()
group f by f into letterfrequency
select letterfrequency;
foreach (var f in frequency)
{
Console.WriteLine($"{f.Key}{f.Count()}");
}
Here is the output:
t1 r2 e1 i1 l2
Output needed:
e1i1l2r2t1
Can't figure out how to sort properly. Any thoughts to what I'm doing wrong?
c# linq
c# linq
edited Nov 20 '18 at 14:59
Dayan
4,26583064
4,26583064
asked Nov 20 '18 at 14:56
MBrewersMBrewers
617
617
1
frequency = frequency.OrderBy(x => x.Count);
– Ryan Wilson
Nov 20 '18 at 14:58
You simply want to order byletterfrequency.Key
, then byletterfrequency.Count()
? Given your grouping, the latter will not matter.
– CodeCaster
Nov 20 '18 at 14:59
5
Can you explain why you wrote an unnecessaryToList
in there? I am interested to learn why people write unnecessary code because it helps me design APIs that lead developers to avoid writing unnecessary code.
– Eric Lippert
Nov 20 '18 at 15:04
By "followed by" do you mean print out the key and then follow it by the count? Or do you mean that the ordering relation depends on the count? If the latter, how does that ever make a difference?
– Eric Lippert
Nov 20 '18 at 15:08
1
The ToList() was one of the ways I was trying sort, originally, because I couldn't get it to work properly. I know, bad coding but I'm trying:) Many thanks
– MBrewers
Nov 20 '18 at 15:53
|
show 1 more comment
1
frequency = frequency.OrderBy(x => x.Count);
– Ryan Wilson
Nov 20 '18 at 14:58
You simply want to order byletterfrequency.Key
, then byletterfrequency.Count()
? Given your grouping, the latter will not matter.
– CodeCaster
Nov 20 '18 at 14:59
5
Can you explain why you wrote an unnecessaryToList
in there? I am interested to learn why people write unnecessary code because it helps me design APIs that lead developers to avoid writing unnecessary code.
– Eric Lippert
Nov 20 '18 at 15:04
By "followed by" do you mean print out the key and then follow it by the count? Or do you mean that the ordering relation depends on the count? If the latter, how does that ever make a difference?
– Eric Lippert
Nov 20 '18 at 15:08
1
The ToList() was one of the ways I was trying sort, originally, because I couldn't get it to work properly. I know, bad coding but I'm trying:) Many thanks
– MBrewers
Nov 20 '18 at 15:53
1
1
frequency = frequency.OrderBy(x => x.Count);
– Ryan Wilson
Nov 20 '18 at 14:58
frequency = frequency.OrderBy(x => x.Count);
– Ryan Wilson
Nov 20 '18 at 14:58
You simply want to order by
letterfrequency.Key
, then by letterfrequency.Count()
? Given your grouping, the latter will not matter.– CodeCaster
Nov 20 '18 at 14:59
You simply want to order by
letterfrequency.Key
, then by letterfrequency.Count()
? Given your grouping, the latter will not matter.– CodeCaster
Nov 20 '18 at 14:59
5
5
Can you explain why you wrote an unnecessary
ToList
in there? I am interested to learn why people write unnecessary code because it helps me design APIs that lead developers to avoid writing unnecessary code.– Eric Lippert
Nov 20 '18 at 15:04
Can you explain why you wrote an unnecessary
ToList
in there? I am interested to learn why people write unnecessary code because it helps me design APIs that lead developers to avoid writing unnecessary code.– Eric Lippert
Nov 20 '18 at 15:04
By "followed by" do you mean print out the key and then follow it by the count? Or do you mean that the ordering relation depends on the count? If the latter, how does that ever make a difference?
– Eric Lippert
Nov 20 '18 at 15:08
By "followed by" do you mean print out the key and then follow it by the count? Or do you mean that the ordering relation depends on the count? If the latter, how does that ever make a difference?
– Eric Lippert
Nov 20 '18 at 15:08
1
1
The ToList() was one of the ways I was trying sort, originally, because I couldn't get it to work properly. I know, bad coding but I'm trying:) Many thanks
– MBrewers
Nov 20 '18 at 15:53
The ToList() was one of the ways I was trying sort, originally, because I couldn't get it to work properly. I know, bad coding but I'm trying:) Many thanks
– MBrewers
Nov 20 '18 at 15:53
|
show 1 more comment
3 Answers
3
active
oldest
votes
The thing you're missing is adding an orderby
to your LINQ statement:
var frequency = from f in "trreill"
group f by f into letterfrequency
orderby letterfrequency.Key
select new
{
Letter = letterfrequency.Key,
Frequency = letterfrequency.Count()
};
foreach (var f in frequency)
{
Console.WriteLine($"{f.Letter}{f.Frequency}");
}
The letterfrequency
has a property called Key
which contains the letter for each group, so adding orderby letterfrequency.Key
sorts the results to give you the output you're after:
e1
i1
l2
r2
t1
I've also tweaked the result of the query slightly (simply to show that it's something that's possible) to generate a new anonymous type that contains the Letter and the Frequency as named properties. This makes the code in the Console.WriteLine
a littl clearer as the properties being used are called Letter
and Frequency
rather than Key
and the Count()
method.
Turning it up to 11, using C# 7.0 Tuples
If you're using C# 7.0, you could replace the use of an anonymous type with Tuples, that means that the code would now look like this:
var frequency = from f in "trreill"
group f by f into letterfrequency
orderby letterfrequency.Key
select
(
Letter: letterfrequency.Key,
Frequency: letterfrequency.Count()
);
foreach (var (Letter, Frequency) in frequency)
{
Console.WriteLine($"{Letter}{Frequency}");
}
I've blogged about this, and there are plenty of other resources that describe them, including this Stackoverflow question that asks 'Are C# anonymous types redundant in C# 7', if you want a deeper dive into Tuples, what they are, how they work and why/when you might want to use them in preference to anonymous types.
.ToList()
is redundant
– Dmitry Bychenko
Nov 20 '18 at 15:06
2
Though this is good, it could be improved in two small ways. (1) remove the unnecessary ToList, and (2) in C# 7, select into a tuple rather than an anonymous type.
– Eric Lippert
Nov 20 '18 at 15:09
add a comment |
Just sort your list as following:
foreach (var f in frequency.OrderBy(item=>item.Key))
{
Console.WriteLine($"{f.Key}{f.Count()}");
}
add a comment |
I would select into an anonymous object:
var frequency = from f in "trreill"
group f by f into letterfrequency
select new { Key = letterfrequency.Key, Count = letterfrequency.Count() };
Then I would order it like so:
foreach (var f in frequency.OrderBy(x => x.Key))
{
Console.WriteLine($"{f.Key}{f.Count}");
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395709%2fc-sharp-sort-string-alphabetically-followed-by-frequency-of-occurrence%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The thing you're missing is adding an orderby
to your LINQ statement:
var frequency = from f in "trreill"
group f by f into letterfrequency
orderby letterfrequency.Key
select new
{
Letter = letterfrequency.Key,
Frequency = letterfrequency.Count()
};
foreach (var f in frequency)
{
Console.WriteLine($"{f.Letter}{f.Frequency}");
}
The letterfrequency
has a property called Key
which contains the letter for each group, so adding orderby letterfrequency.Key
sorts the results to give you the output you're after:
e1
i1
l2
r2
t1
I've also tweaked the result of the query slightly (simply to show that it's something that's possible) to generate a new anonymous type that contains the Letter and the Frequency as named properties. This makes the code in the Console.WriteLine
a littl clearer as the properties being used are called Letter
and Frequency
rather than Key
and the Count()
method.
Turning it up to 11, using C# 7.0 Tuples
If you're using C# 7.0, you could replace the use of an anonymous type with Tuples, that means that the code would now look like this:
var frequency = from f in "trreill"
group f by f into letterfrequency
orderby letterfrequency.Key
select
(
Letter: letterfrequency.Key,
Frequency: letterfrequency.Count()
);
foreach (var (Letter, Frequency) in frequency)
{
Console.WriteLine($"{Letter}{Frequency}");
}
I've blogged about this, and there are plenty of other resources that describe them, including this Stackoverflow question that asks 'Are C# anonymous types redundant in C# 7', if you want a deeper dive into Tuples, what they are, how they work and why/when you might want to use them in preference to anonymous types.
.ToList()
is redundant
– Dmitry Bychenko
Nov 20 '18 at 15:06
2
Though this is good, it could be improved in two small ways. (1) remove the unnecessary ToList, and (2) in C# 7, select into a tuple rather than an anonymous type.
– Eric Lippert
Nov 20 '18 at 15:09
add a comment |
The thing you're missing is adding an orderby
to your LINQ statement:
var frequency = from f in "trreill"
group f by f into letterfrequency
orderby letterfrequency.Key
select new
{
Letter = letterfrequency.Key,
Frequency = letterfrequency.Count()
};
foreach (var f in frequency)
{
Console.WriteLine($"{f.Letter}{f.Frequency}");
}
The letterfrequency
has a property called Key
which contains the letter for each group, so adding orderby letterfrequency.Key
sorts the results to give you the output you're after:
e1
i1
l2
r2
t1
I've also tweaked the result of the query slightly (simply to show that it's something that's possible) to generate a new anonymous type that contains the Letter and the Frequency as named properties. This makes the code in the Console.WriteLine
a littl clearer as the properties being used are called Letter
and Frequency
rather than Key
and the Count()
method.
Turning it up to 11, using C# 7.0 Tuples
If you're using C# 7.0, you could replace the use of an anonymous type with Tuples, that means that the code would now look like this:
var frequency = from f in "trreill"
group f by f into letterfrequency
orderby letterfrequency.Key
select
(
Letter: letterfrequency.Key,
Frequency: letterfrequency.Count()
);
foreach (var (Letter, Frequency) in frequency)
{
Console.WriteLine($"{Letter}{Frequency}");
}
I've blogged about this, and there are plenty of other resources that describe them, including this Stackoverflow question that asks 'Are C# anonymous types redundant in C# 7', if you want a deeper dive into Tuples, what they are, how they work and why/when you might want to use them in preference to anonymous types.
.ToList()
is redundant
– Dmitry Bychenko
Nov 20 '18 at 15:06
2
Though this is good, it could be improved in two small ways. (1) remove the unnecessary ToList, and (2) in C# 7, select into a tuple rather than an anonymous type.
– Eric Lippert
Nov 20 '18 at 15:09
add a comment |
The thing you're missing is adding an orderby
to your LINQ statement:
var frequency = from f in "trreill"
group f by f into letterfrequency
orderby letterfrequency.Key
select new
{
Letter = letterfrequency.Key,
Frequency = letterfrequency.Count()
};
foreach (var f in frequency)
{
Console.WriteLine($"{f.Letter}{f.Frequency}");
}
The letterfrequency
has a property called Key
which contains the letter for each group, so adding orderby letterfrequency.Key
sorts the results to give you the output you're after:
e1
i1
l2
r2
t1
I've also tweaked the result of the query slightly (simply to show that it's something that's possible) to generate a new anonymous type that contains the Letter and the Frequency as named properties. This makes the code in the Console.WriteLine
a littl clearer as the properties being used are called Letter
and Frequency
rather than Key
and the Count()
method.
Turning it up to 11, using C# 7.0 Tuples
If you're using C# 7.0, you could replace the use of an anonymous type with Tuples, that means that the code would now look like this:
var frequency = from f in "trreill"
group f by f into letterfrequency
orderby letterfrequency.Key
select
(
Letter: letterfrequency.Key,
Frequency: letterfrequency.Count()
);
foreach (var (Letter, Frequency) in frequency)
{
Console.WriteLine($"{Letter}{Frequency}");
}
I've blogged about this, and there are plenty of other resources that describe them, including this Stackoverflow question that asks 'Are C# anonymous types redundant in C# 7', if you want a deeper dive into Tuples, what they are, how they work and why/when you might want to use them in preference to anonymous types.
The thing you're missing is adding an orderby
to your LINQ statement:
var frequency = from f in "trreill"
group f by f into letterfrequency
orderby letterfrequency.Key
select new
{
Letter = letterfrequency.Key,
Frequency = letterfrequency.Count()
};
foreach (var f in frequency)
{
Console.WriteLine($"{f.Letter}{f.Frequency}");
}
The letterfrequency
has a property called Key
which contains the letter for each group, so adding orderby letterfrequency.Key
sorts the results to give you the output you're after:
e1
i1
l2
r2
t1
I've also tweaked the result of the query slightly (simply to show that it's something that's possible) to generate a new anonymous type that contains the Letter and the Frequency as named properties. This makes the code in the Console.WriteLine
a littl clearer as the properties being used are called Letter
and Frequency
rather than Key
and the Count()
method.
Turning it up to 11, using C# 7.0 Tuples
If you're using C# 7.0, you could replace the use of an anonymous type with Tuples, that means that the code would now look like this:
var frequency = from f in "trreill"
group f by f into letterfrequency
orderby letterfrequency.Key
select
(
Letter: letterfrequency.Key,
Frequency: letterfrequency.Count()
);
foreach (var (Letter, Frequency) in frequency)
{
Console.WriteLine($"{Letter}{Frequency}");
}
I've blogged about this, and there are plenty of other resources that describe them, including this Stackoverflow question that asks 'Are C# anonymous types redundant in C# 7', if you want a deeper dive into Tuples, what they are, how they work and why/when you might want to use them in preference to anonymous types.
edited Nov 22 '18 at 6:27
answered Nov 20 '18 at 15:00
RobRob
37.7k21103131
37.7k21103131
.ToList()
is redundant
– Dmitry Bychenko
Nov 20 '18 at 15:06
2
Though this is good, it could be improved in two small ways. (1) remove the unnecessary ToList, and (2) in C# 7, select into a tuple rather than an anonymous type.
– Eric Lippert
Nov 20 '18 at 15:09
add a comment |
.ToList()
is redundant
– Dmitry Bychenko
Nov 20 '18 at 15:06
2
Though this is good, it could be improved in two small ways. (1) remove the unnecessary ToList, and (2) in C# 7, select into a tuple rather than an anonymous type.
– Eric Lippert
Nov 20 '18 at 15:09
.ToList()
is redundant– Dmitry Bychenko
Nov 20 '18 at 15:06
.ToList()
is redundant– Dmitry Bychenko
Nov 20 '18 at 15:06
2
2
Though this is good, it could be improved in two small ways. (1) remove the unnecessary ToList, and (2) in C# 7, select into a tuple rather than an anonymous type.
– Eric Lippert
Nov 20 '18 at 15:09
Though this is good, it could be improved in two small ways. (1) remove the unnecessary ToList, and (2) in C# 7, select into a tuple rather than an anonymous type.
– Eric Lippert
Nov 20 '18 at 15:09
add a comment |
Just sort your list as following:
foreach (var f in frequency.OrderBy(item=>item.Key))
{
Console.WriteLine($"{f.Key}{f.Count()}");
}
add a comment |
Just sort your list as following:
foreach (var f in frequency.OrderBy(item=>item.Key))
{
Console.WriteLine($"{f.Key}{f.Count()}");
}
add a comment |
Just sort your list as following:
foreach (var f in frequency.OrderBy(item=>item.Key))
{
Console.WriteLine($"{f.Key}{f.Count()}");
}
Just sort your list as following:
foreach (var f in frequency.OrderBy(item=>item.Key))
{
Console.WriteLine($"{f.Key}{f.Count()}");
}
answered Nov 20 '18 at 15:02
Ehsan.SaradarEhsan.Saradar
46338
46338
add a comment |
add a comment |
I would select into an anonymous object:
var frequency = from f in "trreill"
group f by f into letterfrequency
select new { Key = letterfrequency.Key, Count = letterfrequency.Count() };
Then I would order it like so:
foreach (var f in frequency.OrderBy(x => x.Key))
{
Console.WriteLine($"{f.Key}{f.Count}");
}
add a comment |
I would select into an anonymous object:
var frequency = from f in "trreill"
group f by f into letterfrequency
select new { Key = letterfrequency.Key, Count = letterfrequency.Count() };
Then I would order it like so:
foreach (var f in frequency.OrderBy(x => x.Key))
{
Console.WriteLine($"{f.Key}{f.Count}");
}
add a comment |
I would select into an anonymous object:
var frequency = from f in "trreill"
group f by f into letterfrequency
select new { Key = letterfrequency.Key, Count = letterfrequency.Count() };
Then I would order it like so:
foreach (var f in frequency.OrderBy(x => x.Key))
{
Console.WriteLine($"{f.Key}{f.Count}");
}
I would select into an anonymous object:
var frequency = from f in "trreill"
group f by f into letterfrequency
select new { Key = letterfrequency.Key, Count = letterfrequency.Count() };
Then I would order it like so:
foreach (var f in frequency.OrderBy(x => x.Key))
{
Console.WriteLine($"{f.Key}{f.Count}");
}
edited Nov 20 '18 at 15:06
answered Nov 20 '18 at 15:02
Thomas SchremserThomas Schremser
1,79762128
1,79762128
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395709%2fc-sharp-sort-string-alphabetically-followed-by-frequency-of-occurrence%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
frequency = frequency.OrderBy(x => x.Count);
– Ryan Wilson
Nov 20 '18 at 14:58
You simply want to order by
letterfrequency.Key
, then byletterfrequency.Count()
? Given your grouping, the latter will not matter.– CodeCaster
Nov 20 '18 at 14:59
5
Can you explain why you wrote an unnecessary
ToList
in there? I am interested to learn why people write unnecessary code because it helps me design APIs that lead developers to avoid writing unnecessary code.– Eric Lippert
Nov 20 '18 at 15:04
By "followed by" do you mean print out the key and then follow it by the count? Or do you mean that the ordering relation depends on the count? If the latter, how does that ever make a difference?
– Eric Lippert
Nov 20 '18 at 15:08
1
The ToList() was one of the ways I was trying sort, originally, because I couldn't get it to work properly. I know, bad coding but I'm trying:) Many thanks
– MBrewers
Nov 20 '18 at 15:53