1
$\begingroup$

This question already has an answer here:

I have a list of data, I tried to SortBy it in two equivalent way, but the result is different.

The data variable is something like

{{"2018-05-14", 6.184292`, 6.717337`}, {"2019-01-10", -3.931206`, 
  0.992981`}, {"2018-09-03", 10.06036`, 2.15906`},...}

First way, I sort it like this

SortBy[data, #[[2]] &][[;; , -1]][[-4 ;; -1]]

it gives

{2.15906, 1.86386, 4.42245, 1.93505}

Second way, I sort it like this

Transpose[SortBy[data[[;; , {2, 3}]], First]][[2]][[-4 ;; -1]]

I naively thought this should give the same result as the first way. But it gives

{1.86386, 2.15906, 4.42245, 1.93505}

I can not figure out why, a limited test seems to show that the first column of string affect the sorting result. But I really don't know why. Is it a bug?

Is there some workaround to get stable sorting result with SortBy?

PS:

the full data variable is as below

data = {{"2018-05-14", 6.184292`, 
   6.717337`}, {"2019-01-10", -3.931206`, 0.992981`}, {"2018-09-03", 
   10.06036`, 2.15906`}, {"2017-04-13", 10.00603`, 
   5.872783`}, {"2017-06-23", -3.278692`, 3.102502`}, {"2019-03-01", 
   10.`, 11.753364`}, {"2017-03-23", 2.001785`, 
   12.764118`}, {"2017-05-17", 3.229816`, 
   2.436859`}, {"2019-02-27", -5.761638`, 10.630857`}, {"2018-11-19", 
   2.238807`, 7.370008`}, {"2018-01-25", -4.185268`, 
   8.715547`}, {"2019-02-14", 9.981172`, 1.042997`}, {"2017-03-29", 
   10.00949`, 5.63856`}, {"2017-03-17", 10.00622`, 
   4.482422`}, {"2018-08-09", 9.015448`, 
   15.249212`}, {"2018-11-07", -6.416273`, 3.560157`}, {"2018-01-19", 
   6.864568`, 9.58189`}, {"2019-01-16", -4.835393`, 
   2.745814`}, {"2017-01-13", 10.01484`, 
   7.819988`}, {"2017-12-29", -4.930658`, 2.331498`}, {"2017-11-14", 
   10.02825`, 3.705003`}, {"2018-11-01", 2.84006`, 
   5.798435`}, {"2018-10-18", -9.730444`, 6.403442`}, {"2018-11-19", 
   9.99044`, 4.594504`}, {"2017-04-11", 9.989251`, 
   8.665032`}, {"2018-01-24", 9.973293`, 4.113164`}, {"2018-05-31", 
   9.979642`, 5.436269`}, {"2017-12-18", -1.983477`, 
   1.464409`}, {"2018-07-27", -8.452954`, 1.244757`}, {"2017-05-31", 
   10.01268`, 13.419636`}, {"2017-08-29", 9.999996`, 
   12.275278`}, {"2018-07-10", -2.012577`, 1.988242`}, {"2018-04-12", 
   1.820249`, 5.015844`}, {"2017-12-13", 9.981937`, 
   14.314185`}, {"2017-03-03", 10.00437`, 
   12.55615`}, {"2017-10-18", -7.613326`, 11.405841`}, {"2018-09-14", 
   9.611231`, 11.38485`}, {"2018-01-08", 10.00422`, 
   7.481106`}, {"2018-03-13", 0.`, 2.951733`}, {"2018-07-30", 
   1.284959`, 13.011311`}, {"2018-10-08", -2.857145`, 
   2.654354`}, {"2018-07-03", 0.74074`, 1.967703`}, {"2018-11-16", 
   10.00667`, 4.859302`}, {"2018-02-22", 3.131317`, 
   3.019479`}, {"2017-02-13", 0.285714`, 2.303895`}, {"2018-09-14", 
   10.07194`, 1.93505`}, {"2017-03-13", -2.458607`, 
   6.74963`}, {"2019-02-14", -1.994792`, 
   7.38112`}, {"2017-08-29", -1.603944`, 
   12.610766`}, {"2017-07-18", -0.042311`, 12.598572`}, {"2017-09-25",
    9.984406`, 3.539927`}, {"2018-08-21", -0.038359`, 
   4.686513`}, {"2017-05-03", 7.805756`, 10.247516`}, {"2019-01-25", 
   9.987517`, 2.061129`}, {"2017-04-07", 10.02865`, 
   9.404674`}, {"2019-02-25", 9.95608`, 1.513823`}, {"2019-01-07", 
   10.06036`, 1.863862`}, {"2019-02-26", 9.302326`, 
   4.515664`}, {"2018-01-24", 9.986224`, 
   9.208368`}, {"2018-11-12", -2.688811`, 4.733748`}, {"2017-04-26", 
   10.00693`, 6.822327`}, {"2019-01-08", -3.516175`, 
   2.578293`}, {"2018-05-16", 9.999996`, 6.363988`}, {"2018-03-14", 
   5.83941`, 3.25484`}, {"2017-03-03", -3.935598`, 
   10.438454`}, {"2018-05-16", 9.994114`, 
   3.888683`}, {"2018-10-11", -4.590159`, 5.272433`}, {"2017-02-15", 
   9.99465`, 22.858054`}, {"2018-03-06", 0.906059`, 
   17.653249`}, {"2018-06-25", 9.960545`, 5.661866`}, {"2018-11-16", 
   1.76057`, 1.704593`}, {"2018-11-08", 0.480257`, 
   3.426226`}, {"2019-01-08", 10.01789`, 
   2.411736`}, {"2018-03-13", -2.691887`, 
   4.517372`}, {"2017-06-19", -1.841573`, 1042.81686`}, {"2017-08-09",
    0.619013`, 6.911994`}, {"2018-11-09", 10.`, 
   1.827005`}, {"2019-02-27", 5.109066`, 7.227035`}, {"2017-11-08", 
   0.639726`, 2.921231`}, {"2018-02-23", 8.354053`, 
   4.745265`}, {"2018-09-12", 10.01032`, 4.207602`}, {"2018-07-04", 
   9.644977`, 15.399009`}, {"2018-10-23", -0.913241`, 
   1.038415`}, {"2017-01-09", 9.992285`, 
   13.255343`}, {"2017-04-13", -2.114163`, 3.245738`}, {"2017-04-10", 
   9.981172`, 3.618146`}, {"2018-11-20", -3.286393`, 
   1.947669`}, {"2018-08-14", -8.880456`, 
   11.656775`}, {"2019-02-26", -3.758057`, 5.712208`}, {"2017-09-20", 
   9.980683`, 7.426913`}, {"2019-02-12", 3.549062`, 
   2.218405`}, {"2018-02-26", 6.195124`, 3.583551`}, {"2018-04-25", 
   9.998705`, 10.206493`}, {"2018-08-06", 10.07025`, 
   4.422451`}, {"2017-02-07", -0.06693`, 8.167308`}, {"2017-09-19", 
   10.00868`, 12.952418`}, {"2018-08-31", 7.233498`, 
   7.145747`}, {"2018-04-25", 0.940438`, 
   8.010683`}, {"2017-01-19", -0.53545`, 15.877556`}, {"2018-02-13", 
   3.758292`, 5.08243`}, {"2017-01-04", 9.974427`, 
   2.762563`}, {"2018-06-19", 9.956711`, 3.869365`}, {"2018-04-09", 
   1.737808`, 22.755046`}, {"2017-07-12", 2.594803`, 
   3.235668`}, {"2019-01-03", 10.01632`, 3.677325`}, {"2019-02-12", 
   2.38095`, 1.796339`}, {"2017-02-27", 9.974427`, 
   8.576878`}, {"2017-01-17", 4.350225`, 2.464616`}, {"2018-03-21", 
   7.066045`, 1.714317`}, {"2018-05-31", 0.`, 
   2.337773`}, {"2017-10-25", 9.99025`, 7.488821`}, {"2017-05-12", 
   9.980425`, 6.785923`}, {"2017-01-17", 10.01296`, 
   8.248838`}, {"2018-08-10", 0.`, 4.274576`}, {"2017-08-31", 
   0.23113`, 2.438556`}, {"2018-06-12", 4.208693`, 
   4.737961`}, {"2018-11-22", 9.999997`, 1.924548`}, {"2017-04-28", 
   10.00419`, 10.937031`}, {"2017-08-07", 10.0051`, 
   7.340555`}, {"2018-05-21", 3.415557`, 1.340161`}}
$\endgroup$

marked as duplicate by MarcoB, Alexey Popkov, m_goldberg, Henrik Schumacher list-manipulation Mar 4 at 7:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ @MarcoB Thank you for providing the useful link $\endgroup$ – matheorem Mar 4 at 2:44
3
$\begingroup$

Is there some workaround to get stable sorting result with SortBy?

Yes, just wrap the second argument by List:

SortBy[data, {#[[2]] &}][[;; , -1]][[-4 ;; -1]]
{2.15906, 1.86386, 4.42245, 1.93505}
Transpose[
   SortBy[data[[;; , {2, 3}]], {First}]][[2]][[-4 ;; -1]]
{2.15906, 1.86386, 4.42245, 1.93505}

Actually your question is a duplicate of the following old SO question which contains detailed answer:

$\endgroup$
  • $\begingroup$ OK, then what is the difference? Why this works? $\endgroup$ – matheorem Mar 4 at 1:32
  • $\begingroup$ @matheorem I updated the answer. $\endgroup$ – Alexey Popkov Mar 4 at 1:34
  • $\begingroup$ Thank you so much for the information. I finally understand. $\endgroup$ – matheorem Mar 4 at 2:39

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