11

In order to sort alphanumeric-as-string data of the form {"T3", "T14", "T1", "E2"}, so that "T14" comes after "T3", SortBy requires the tiebreaker function list:

{StringTake[#, 1] &, ToExpression@StringDrop[#, 1] &}

Which works as intended when this expression is inserted literally in SortBy. However, defining a utility function

mySort[x_String]:={StringTake[x, 1] , ToExpression@StringDrop[x, 1]}

doesn't work since the output is a list of expressions rather than a list of functions.

The alternative - to define a function via

mySort := {StringTake[#, 1] &, ToExpression@StringDrop[#, 1] &}

only works when the list to be sorted is 1-dimensional (as above) but not with lists of the form data2={"T3"->a, "T14"->b, "T1"->c, "E2"->d} where it is necessary to use SortBy[data2,mySort[#[[1]]]&].

Any alternatives that will work for general expressions?

alancalvitti
  • 15,143
  • 3
  • 27
  • 92

1 Answers1

15

I think this question admits an elegant solution. Here is my attempt: define a special wrapper:

ClearAll[sortFun];
sortFun /: SortBy[expr_, sortFun[funs_List, partFun_]] :=
   SortBy[expr, Map[Composition[#, partFun] &, funs]];

Now,

mySort := {StringTake[#, 1] &, ToExpression@StringDrop[#, 1] &}

and

SortBy[{"T3","T14","T1","E2"}, sortFun[mySort, Identity]]

(*   {E2,T1,T3,T14}  *)

while

SortBy[{"T3"->a,"T14"->b,"T1"->c,"E2"->d}, sortFun[mySort, First]]

(*  {E2->d,T1->c,T3->a,T14->b} *)

EDIT

Perhaps more elegantly

ClearAll[sortFun];
sortFun /: SortBy[expr_, sortFun[funs_List, partFun_]] :=
   SortBy[expr, Thread[Composition[funs, partFun]]];

and just for fun, another version:

ClearAll[sortFun];
sortFun /: call : SortBy[expr_, sortFun[funs_List, partFun_]] :=
   Block[{sortFun = Thread[Composition[##]] &}, call];
Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420
  • Superior to mine, +1 – Rojo Jun 20 '12 at 20:09
  • @Rojo Thanks :) We seem to be thinking very similarly :) – Leonid Shifrin Jun 20 '12 at 20:10
  • @Leonid, you're a wizard, but I would not call this solution elegant despite your elegant code. Who wants to pass around sortFun and all that. This is something WRI should tackle. – alancalvitti Jun 20 '12 at 20:22
  • 1
    @alancalvitti I actually don't find carrying sortFun around a big burden. It is a very general wrapper, and it minimally changes the syntax. One can use some shorter name if sortFun is too long. One could also add rules to SortBy, but I'd rather not do it. – Leonid Shifrin Jun 20 '12 at 20:24
  • sortFun[mySort, Identity] and sortFun[mySort, First] burn/spill highly important syntactic sugar. If the 1st can be somehow massaged into just sortFun, and the 2nd into sortFun[[1]], that would be elegant...But I'll accept your answer & +1 it. – alancalvitti Jun 20 '12 at 21:44
  • 1
    @alancalvitti Thanks for the accept. As for elegance, it seems that we have somewhat different perceptions on that (which is just fine, this is a very subjective matter). I feel that the first solution in the edit is the most elegant, and will be hard-pressed to see anything simpler than that. – Leonid Shifrin Jun 20 '12 at 21:47
  • Simpler from the user perspective is related to usability. Can you imagine an implementation of SortBy that recognizes List of Functions and maps Parts to Slot accordingly? Perhaps that's what you meant by the Rule approach. – alancalvitti Jun 20 '12 at 22:13
  • 1
    @alancalvitti This will then be either a more narrow, point solution (since functions can also be represented as symbols, composite heads with sub-values, etc), or will be really complex and would require compile stage to be efficient. I do not exclude that this is possible, but I think that my solution captures the mathematical essence of the problem, which is, function's composition. And when possible, I value mathematical clarity over what may look like a better usability, because a real usability is in supporting the right abstractions. – Leonid Shifrin Jun 20 '12 at 22:18
  • There's no question that you've solved the mathematical essence of the problem. But the solution does not preserve the transparent syntax SortBy displays for simple (ie, no tiebreaker) sort functions - that's all I meant by my earlier comments. Why don't we use assembly language or R? They're Turing complete, so they all capture the mathematics. – alancalvitti Jun 21 '12 at 05:17
  • @J.M. Thanks. I used the opportunity to use Composition in a place where it seems natural (which is what you also suggested in that comment). – Leonid Shifrin Aug 25 '12 at 18:55