Sorting by nested Dictionary value?












1















var userInformation = new Dictionary<string, Dictionary<string, int>>();


I need a new dictionary that equals this one, but is sorted first by key then by the value's value. I tried:



var resultInformation = userInformation.OrderBy(k => k.Key).ThenBy(v => v.Value.OrderByDescending(x => x.Value));


I tried a couple of other methods but no effect.










share|improve this question




















  • 1





    Dictionaries aren't sorted (in the traditional sense).

    – Kenneth K.
    Nov 15 '18 at 21:55











  • Yes, my bad. I need to order them. Just my natural way of expression.

    – Dimitar Mitranov
    Nov 15 '18 at 21:56











  • You want to sort by which value in the value?

    – SLaks
    Nov 15 '18 at 21:56











  • First I want to Order resultInformation by userInformation's keys and then by userInformation's nested dictionary value's values. That would be the integer values of the nested dictionary, not the keys. They represent a grade which varies from 0 to 100. The key of the dictionary itself is username of the student that applies the score. the string in the nested represents the course in which they apply the score and the integer is their score. I need to print on a console first the names order in alphabetical order and then for each user his scores in different courses ordered by descending.

    – Dimitar Mitranov
    Nov 15 '18 at 22:04
















1















var userInformation = new Dictionary<string, Dictionary<string, int>>();


I need a new dictionary that equals this one, but is sorted first by key then by the value's value. I tried:



var resultInformation = userInformation.OrderBy(k => k.Key).ThenBy(v => v.Value.OrderByDescending(x => x.Value));


I tried a couple of other methods but no effect.










share|improve this question




















  • 1





    Dictionaries aren't sorted (in the traditional sense).

    – Kenneth K.
    Nov 15 '18 at 21:55











  • Yes, my bad. I need to order them. Just my natural way of expression.

    – Dimitar Mitranov
    Nov 15 '18 at 21:56











  • You want to sort by which value in the value?

    – SLaks
    Nov 15 '18 at 21:56











  • First I want to Order resultInformation by userInformation's keys and then by userInformation's nested dictionary value's values. That would be the integer values of the nested dictionary, not the keys. They represent a grade which varies from 0 to 100. The key of the dictionary itself is username of the student that applies the score. the string in the nested represents the course in which they apply the score and the integer is their score. I need to print on a console first the names order in alphabetical order and then for each user his scores in different courses ordered by descending.

    – Dimitar Mitranov
    Nov 15 '18 at 22:04














1












1








1








var userInformation = new Dictionary<string, Dictionary<string, int>>();


I need a new dictionary that equals this one, but is sorted first by key then by the value's value. I tried:



var resultInformation = userInformation.OrderBy(k => k.Key).ThenBy(v => v.Value.OrderByDescending(x => x.Value));


I tried a couple of other methods but no effect.










share|improve this question
















var userInformation = new Dictionary<string, Dictionary<string, int>>();


I need a new dictionary that equals this one, but is sorted first by key then by the value's value. I tried:



var resultInformation = userInformation.OrderBy(k => k.Key).ThenBy(v => v.Value.OrderByDescending(x => x.Value));


I tried a couple of other methods but no effect.







c# dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 21:57









StriplingWarrior

109k20183235




109k20183235










asked Nov 15 '18 at 21:53









Dimitar MitranovDimitar Mitranov

83




83








  • 1





    Dictionaries aren't sorted (in the traditional sense).

    – Kenneth K.
    Nov 15 '18 at 21:55











  • Yes, my bad. I need to order them. Just my natural way of expression.

    – Dimitar Mitranov
    Nov 15 '18 at 21:56











  • You want to sort by which value in the value?

    – SLaks
    Nov 15 '18 at 21:56











  • First I want to Order resultInformation by userInformation's keys and then by userInformation's nested dictionary value's values. That would be the integer values of the nested dictionary, not the keys. They represent a grade which varies from 0 to 100. The key of the dictionary itself is username of the student that applies the score. the string in the nested represents the course in which they apply the score and the integer is their score. I need to print on a console first the names order in alphabetical order and then for each user his scores in different courses ordered by descending.

    – Dimitar Mitranov
    Nov 15 '18 at 22:04














  • 1





    Dictionaries aren't sorted (in the traditional sense).

    – Kenneth K.
    Nov 15 '18 at 21:55











  • Yes, my bad. I need to order them. Just my natural way of expression.

    – Dimitar Mitranov
    Nov 15 '18 at 21:56











  • You want to sort by which value in the value?

    – SLaks
    Nov 15 '18 at 21:56











  • First I want to Order resultInformation by userInformation's keys and then by userInformation's nested dictionary value's values. That would be the integer values of the nested dictionary, not the keys. They represent a grade which varies from 0 to 100. The key of the dictionary itself is username of the student that applies the score. the string in the nested represents the course in which they apply the score and the integer is their score. I need to print on a console first the names order in alphabetical order and then for each user his scores in different courses ordered by descending.

    – Dimitar Mitranov
    Nov 15 '18 at 22:04








1




1





Dictionaries aren't sorted (in the traditional sense).

– Kenneth K.
Nov 15 '18 at 21:55





Dictionaries aren't sorted (in the traditional sense).

– Kenneth K.
Nov 15 '18 at 21:55













Yes, my bad. I need to order them. Just my natural way of expression.

– Dimitar Mitranov
Nov 15 '18 at 21:56





Yes, my bad. I need to order them. Just my natural way of expression.

– Dimitar Mitranov
Nov 15 '18 at 21:56













You want to sort by which value in the value?

– SLaks
Nov 15 '18 at 21:56





You want to sort by which value in the value?

– SLaks
Nov 15 '18 at 21:56













First I want to Order resultInformation by userInformation's keys and then by userInformation's nested dictionary value's values. That would be the integer values of the nested dictionary, not the keys. They represent a grade which varies from 0 to 100. The key of the dictionary itself is username of the student that applies the score. the string in the nested represents the course in which they apply the score and the integer is their score. I need to print on a console first the names order in alphabetical order and then for each user his scores in different courses ordered by descending.

– Dimitar Mitranov
Nov 15 '18 at 22:04





First I want to Order resultInformation by userInformation's keys and then by userInformation's nested dictionary value's values. That would be the integer values of the nested dictionary, not the keys. They represent a grade which varies from 0 to 100. The key of the dictionary itself is username of the student that applies the score. the string in the nested represents the course in which they apply the score and the integer is their score. I need to print on a console first the names order in alphabetical order and then for each user his scores in different courses ordered by descending.

– Dimitar Mitranov
Nov 15 '18 at 22:04












1 Answer
1






active

oldest

votes


















1














Dictionaries aren't sorted, but you can easily produce a list/collection of the items in your dictionaries, like so:



var resultInformation = from outer in userInformation
from inner in outer.Value
let data = new { Outer = outer.Key, Inner = inner.Key, Value = inner.Value }
orderby data.Outer, data.Inner, data.Value
select data;


Or the query syntax equivalent:



var resultInformation = userInformation
.SelectMany(i => i.Value, (key, inner) => new { Outer = key, Inner = inner.Key, Value = inner.Value})
.OrderBy(e => e.Outer)
.ThenBy(e => e.Inner)
.ThenBy(e => e.Value);


Update: Based on your clarifying comment, I think what you really want is something more like this:



var resultInformation = 
from student in userInformation
orderby student.Key
select new
{
studentId = student.Key,
courses =
from courseScore in student.Value
orderby courseScore.Value descending
select new {
course = courseScore.Key,
score = courseScore.Value
}
};





share|improve this answer


























  • Thank you for the answers. I believe the third option would serve me great, only if I knew how to print it on the Console Application, ha ha. I am quite new to programming and have yet to study such things as the ones you posted. I kind of get all three of them but with my level of knowledge I could hardly implement them. I have made an exact copy of your third option, but do not know how to print it. There can be multiple students of course and multiple scores and courses.

    – Dimitar Mitranov
    Nov 15 '18 at 22:30






  • 1





    After a little bit of research I realized it was so simple to print them again with a foreach loop. And it works perfectly fine! Thank you very much for the answer :)

    – Dimitar Mitranov
    Nov 15 '18 at 23:45











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%2f53328422%2fsorting-by-nested-dictionary-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Dictionaries aren't sorted, but you can easily produce a list/collection of the items in your dictionaries, like so:



var resultInformation = from outer in userInformation
from inner in outer.Value
let data = new { Outer = outer.Key, Inner = inner.Key, Value = inner.Value }
orderby data.Outer, data.Inner, data.Value
select data;


Or the query syntax equivalent:



var resultInformation = userInformation
.SelectMany(i => i.Value, (key, inner) => new { Outer = key, Inner = inner.Key, Value = inner.Value})
.OrderBy(e => e.Outer)
.ThenBy(e => e.Inner)
.ThenBy(e => e.Value);


Update: Based on your clarifying comment, I think what you really want is something more like this:



var resultInformation = 
from student in userInformation
orderby student.Key
select new
{
studentId = student.Key,
courses =
from courseScore in student.Value
orderby courseScore.Value descending
select new {
course = courseScore.Key,
score = courseScore.Value
}
};





share|improve this answer


























  • Thank you for the answers. I believe the third option would serve me great, only if I knew how to print it on the Console Application, ha ha. I am quite new to programming and have yet to study such things as the ones you posted. I kind of get all three of them but with my level of knowledge I could hardly implement them. I have made an exact copy of your third option, but do not know how to print it. There can be multiple students of course and multiple scores and courses.

    – Dimitar Mitranov
    Nov 15 '18 at 22:30






  • 1





    After a little bit of research I realized it was so simple to print them again with a foreach loop. And it works perfectly fine! Thank you very much for the answer :)

    – Dimitar Mitranov
    Nov 15 '18 at 23:45
















1














Dictionaries aren't sorted, but you can easily produce a list/collection of the items in your dictionaries, like so:



var resultInformation = from outer in userInformation
from inner in outer.Value
let data = new { Outer = outer.Key, Inner = inner.Key, Value = inner.Value }
orderby data.Outer, data.Inner, data.Value
select data;


Or the query syntax equivalent:



var resultInformation = userInformation
.SelectMany(i => i.Value, (key, inner) => new { Outer = key, Inner = inner.Key, Value = inner.Value})
.OrderBy(e => e.Outer)
.ThenBy(e => e.Inner)
.ThenBy(e => e.Value);


Update: Based on your clarifying comment, I think what you really want is something more like this:



var resultInformation = 
from student in userInformation
orderby student.Key
select new
{
studentId = student.Key,
courses =
from courseScore in student.Value
orderby courseScore.Value descending
select new {
course = courseScore.Key,
score = courseScore.Value
}
};





share|improve this answer


























  • Thank you for the answers. I believe the third option would serve me great, only if I knew how to print it on the Console Application, ha ha. I am quite new to programming and have yet to study such things as the ones you posted. I kind of get all three of them but with my level of knowledge I could hardly implement them. I have made an exact copy of your third option, but do not know how to print it. There can be multiple students of course and multiple scores and courses.

    – Dimitar Mitranov
    Nov 15 '18 at 22:30






  • 1





    After a little bit of research I realized it was so simple to print them again with a foreach loop. And it works perfectly fine! Thank you very much for the answer :)

    – Dimitar Mitranov
    Nov 15 '18 at 23:45














1












1








1







Dictionaries aren't sorted, but you can easily produce a list/collection of the items in your dictionaries, like so:



var resultInformation = from outer in userInformation
from inner in outer.Value
let data = new { Outer = outer.Key, Inner = inner.Key, Value = inner.Value }
orderby data.Outer, data.Inner, data.Value
select data;


Or the query syntax equivalent:



var resultInformation = userInformation
.SelectMany(i => i.Value, (key, inner) => new { Outer = key, Inner = inner.Key, Value = inner.Value})
.OrderBy(e => e.Outer)
.ThenBy(e => e.Inner)
.ThenBy(e => e.Value);


Update: Based on your clarifying comment, I think what you really want is something more like this:



var resultInformation = 
from student in userInformation
orderby student.Key
select new
{
studentId = student.Key,
courses =
from courseScore in student.Value
orderby courseScore.Value descending
select new {
course = courseScore.Key,
score = courseScore.Value
}
};





share|improve this answer















Dictionaries aren't sorted, but you can easily produce a list/collection of the items in your dictionaries, like so:



var resultInformation = from outer in userInformation
from inner in outer.Value
let data = new { Outer = outer.Key, Inner = inner.Key, Value = inner.Value }
orderby data.Outer, data.Inner, data.Value
select data;


Or the query syntax equivalent:



var resultInformation = userInformation
.SelectMany(i => i.Value, (key, inner) => new { Outer = key, Inner = inner.Key, Value = inner.Value})
.OrderBy(e => e.Outer)
.ThenBy(e => e.Inner)
.ThenBy(e => e.Value);


Update: Based on your clarifying comment, I think what you really want is something more like this:



var resultInformation = 
from student in userInformation
orderby student.Key
select new
{
studentId = student.Key,
courses =
from courseScore in student.Value
orderby courseScore.Value descending
select new {
course = courseScore.Key,
score = courseScore.Value
}
};






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 22:16

























answered Nov 15 '18 at 22:02









StriplingWarriorStriplingWarrior

109k20183235




109k20183235













  • Thank you for the answers. I believe the third option would serve me great, only if I knew how to print it on the Console Application, ha ha. I am quite new to programming and have yet to study such things as the ones you posted. I kind of get all three of them but with my level of knowledge I could hardly implement them. I have made an exact copy of your third option, but do not know how to print it. There can be multiple students of course and multiple scores and courses.

    – Dimitar Mitranov
    Nov 15 '18 at 22:30






  • 1





    After a little bit of research I realized it was so simple to print them again with a foreach loop. And it works perfectly fine! Thank you very much for the answer :)

    – Dimitar Mitranov
    Nov 15 '18 at 23:45



















  • Thank you for the answers. I believe the third option would serve me great, only if I knew how to print it on the Console Application, ha ha. I am quite new to programming and have yet to study such things as the ones you posted. I kind of get all three of them but with my level of knowledge I could hardly implement them. I have made an exact copy of your third option, but do not know how to print it. There can be multiple students of course and multiple scores and courses.

    – Dimitar Mitranov
    Nov 15 '18 at 22:30






  • 1





    After a little bit of research I realized it was so simple to print them again with a foreach loop. And it works perfectly fine! Thank you very much for the answer :)

    – Dimitar Mitranov
    Nov 15 '18 at 23:45

















Thank you for the answers. I believe the third option would serve me great, only if I knew how to print it on the Console Application, ha ha. I am quite new to programming and have yet to study such things as the ones you posted. I kind of get all three of them but with my level of knowledge I could hardly implement them. I have made an exact copy of your third option, but do not know how to print it. There can be multiple students of course and multiple scores and courses.

– Dimitar Mitranov
Nov 15 '18 at 22:30





Thank you for the answers. I believe the third option would serve me great, only if I knew how to print it on the Console Application, ha ha. I am quite new to programming and have yet to study such things as the ones you posted. I kind of get all three of them but with my level of knowledge I could hardly implement them. I have made an exact copy of your third option, but do not know how to print it. There can be multiple students of course and multiple scores and courses.

– Dimitar Mitranov
Nov 15 '18 at 22:30




1




1





After a little bit of research I realized it was so simple to print them again with a foreach loop. And it works perfectly fine! Thank you very much for the answer :)

– Dimitar Mitranov
Nov 15 '18 at 23:45





After a little bit of research I realized it was so simple to print them again with a foreach loop. And it works perfectly fine! Thank you very much for the answer :)

– Dimitar Mitranov
Nov 15 '18 at 23:45




















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%2f53328422%2fsorting-by-nested-dictionary-value%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

Bressuire

Vorschmack

Quarantine