1
$\begingroup$

I have:

mydata = {{1, "NEBRASKA", 2.7`}, {2, "NORTH DAKOTA", 3.`}, {3, "UTAH",
    3.6`}, {3, "VERMONT", 3.6`}, {5, "HAWAII", 3.7`}}

Now, I can do:

SortBy[mydata,First]

And:

SortBy[mydata,Last]

But how do I sort by the second item in each list?

$\endgroup$
5
  • 2
    $\begingroup$ SortBy[mydata,#[[2]]&] $\endgroup$ – Sjoerd C. de Vries Sep 8 '15 at 21:36
  • 2
    $\begingroup$ SortBy[mydata, First@*Rest] :P $\endgroup$ – chuy Sep 8 '15 at 21:40
  • 2
    $\begingroup$ With version-10 operator forms: mydata // SortBy@Extract@2 $\endgroup$ – Mr.Wizard Sep 8 '15 at 21:42
  • $\begingroup$ Related: (2934) $\endgroup$ – Mr.Wizard Sep 8 '15 at 21:43
  • $\begingroup$ @Mr.Wizard et al, thanks everyone. Very helpful. $\endgroup$ – David Sep 8 '15 at 22:00
2
$\begingroup$

Try

SortBy[mydata,#[[2]]&]

the supplied function should select the second element of each entry as the sort-by value.

$\endgroup$

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