3
$\begingroup$

I have a list

m = {{a, 20}, {b, 10}, {c, 50}, {d, 5}, {e, 100}} 

and I want to apply Sort on this in descending order on numbers only so that it looks like this

m = {{e, 100}, {c, 50}, {a, 20}, {b, 10}, {d, 5}}

How do I do that?

$\endgroup$
  • 1
    $\begingroup$ Take a look at SortBy. $\endgroup$ – Kuba Oct 14 '15 at 15:38
  • 1
    $\begingroup$ SortBy[m,Last] $\endgroup$ – IPoiler Oct 14 '15 at 15:39
  • $\begingroup$ Greetings! To make the most of Mma.SE please take the tour now. Help us to help you, write an excellent question. Edit if improvable, show due diligence, give brief context, include minimum working examples of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$ – rhermans Oct 14 '15 at 15:41
  • $\begingroup$ Your question may be put on-hold as it seems to be off-topic, i.e it is easily found in the documentation and is unlikely to help any future visitors. Don't be discouraged by that cleaning-up policy. Your future good questions are welcome. Learn about common pitfalls here. $\endgroup$ – rhermans Oct 14 '15 at 15:47
9
$\begingroup$

Data

m = {{a, 20}, {b, 10}, {c, 50}, {d, 5} , {e, 100}}

Code

Reverse[SortBy[m, Last]]

or

SortBy[m, Minus@*Last]

or since Mathematica v12

ReverseSortBy[Last][m]
{{e, 100}, {c, 50}, {a, 20}, {b, 10}, {d, 5}}

Where @* stands for Composition

| improve this answer | |
$\endgroup$

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