7 kyu

Sorting Dictionaries

3,548 of 5,322cmgerber

Description:

Python dictionaries are inherently unsorted. So what do you do if you need to sort the contents of a dictionary?

Create a function that returns a sorted list of (key, value) tuples (Javascript: arrays of 2 items).

The list must be sorted by the value and be sorted largest to smallest.

Examples

sort_dict({3:1, 2:2, 1:3}) == [(1,3), (2,2), (3,1)]
sort_dict({1:2, 2:4, 3:6}) == [(3,6), (2,4), (1,2)]
sortDict({3:1, 2:2, 1:3}) == [[1,3], [2,2], [3,1]]
sortDict({1:2, 2:4, 3:6}) == [[3,6], [2,4], [1,2]]
sortDict [(3,1), (2,2), (1,3)] `shouldBe` [(1,3), (2,2), (3,1)]
sortDict [(1,2), (2,4), (3,6)] `shouldBe` [(3,6), (2,4), (1,2)]
Sorting
Lists
Fundamentals

More By Author:

Check out these other kata created by cmgerber

Stats:

CreatedJul 31, 2014
PublishedJul 31, 2014
Warriors Trained10265
Total Skips1051
Total Code Submissions15930
Total Times Completed5322
Python Completions3548
JavaScript Completions1658
Haskell Completions329
Total Stars104
% of votes with a positive feedback rating91% of 574
Total "Very Satisfied" Votes489
Total "Somewhat Satisfied" Votes69
Total "Not Satisfied" Votes16
Ad
Contributors
  • cmgerber Avatar
  • jhoffner Avatar
  • OverZealous Avatar
  • xcthulhu Avatar
  • ZozoFouchtra Avatar
  • bkaes Avatar
  • anter69 Avatar
  • JohanWiltink Avatar
  • Voile Avatar
  • user8436785 Avatar
  • saudiGuy Avatar
Ad