3
$\begingroup$

I am new in the Mathematica community and I have a probably easy question for you regarding how to sort a dataset as the following so that I have sorted the values in the fist row descending from 44 in the first line down to 3 in the last line.

dataset = Dataset[{
  <|"a" -> 6, "b" -> "x", "c" -> {1}|>, 
  <|"a" -> 8, "b" -> "y", "c" -> {2, 3}|>, 
  <|"a" -> 44, "b" -> "z", "c" -> {3}|>, 
  <|"a" -> 4, "b" -> "x", "c" -> {4, 5}|>, 
  <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>, 
  <|"a" -> 3, "b" -> "z", "c" -> {}|>}]
$\endgroup$
  • 2
    $\begingroup$ dataset[SortBy[-#a &]]. $\endgroup$ – Leonid Shifrin Mar 20 '15 at 18:38
  • 1
    $\begingroup$ ..... or dataset[SortBy[a]][Reverse] ;-) $\endgroup$ – mgamer Mar 20 '15 at 18:42
  • $\begingroup$ Perfect!! Thank you very much for the quick response!! $\endgroup$ – Hans Meier Mar 20 '15 at 18:52
  • 1
    $\begingroup$ dataset[SortBy[a] /* Reverse] $\endgroup$ – alancalvitti Mar 20 '15 at 20:07
  • 1
    $\begingroup$ Reverse @ Sort@ ds $\endgroup$ – m_goldberg Mar 21 '15 at 3:08
6
$\begingroup$

Let's gather all the answers in the comments here.

dataset[SortBy[-#a &]]
dataset[SortBy["a"]][Reverse]
dataset[SortBy["a"] /* Reverse]
Reverse @ Sort @ dataset
$\endgroup$
  • $\begingroup$ I corrected SortBy[a] to SortBy["a"]. The former does produce the correct result, but that's just a coincidence. It's in fact equivalent to a simple Sort and the symbol a makes no difference, one could write b or c and it wouldn't change the result. $\endgroup$ – Szabolcs May 17 '15 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.