I'm very new to GAP, and I'm struggling with SortBy. Starting with a list of lists, I want to sort each list by AbsoluteValue in GAP. However, I can only get Sort or SortBy to work outside of a loop. The problem exists already in this tiny example:

gap> A:=[[-5,-3,-1,2,4,6],[-6,-4,-2,1,3,5]];
[ [ -5, -3, -1, 2, 4, 6 ], [ -6, -4, -2, 1, 3, 5 ] ]
gap> sorter:=function(X)
>       for i in [1..Length(X)] do
>       X[i]:=SortBy(X[i],AbsoluteValue);
>       od;
> end;;
gap> sorter(A);
Error, Function Calls: <func> must return a value in
  X[i] := SortBy( X[i], AbsoluteValue ); at *stdin*:24 called from
<function "sorter">( <arguments> )
 called from read-eval loop at line 27 of *stdin*
you can supply one by 'return <value>;'
gap> A[1];A[2];
[ -1, 2, -3, 4, -5, 6 ]
[ -6, -4, -2, 1, 3, 5 ]

It sorts the first list, but apparently doesn't like the second one. I think the error message says that AbsoluteValue doesn't return a value, which is false. What am I missing?

  • $\begingroup$ This is math, vanilla stackexchange is that way -> stackexchange.com $\endgroup$
    – user762914
    Jun 3, 2020 at 20:54
  • $\begingroup$ Ah okay, I thought that GAP was a math-programming thing, but I can try to ask it on the main one. $\endgroup$ Jun 3, 2020 at 20:56
  • $\begingroup$ @EthanKowalenko please see the GAP tag description for other GAP support channels. The number of GAP questions on the main stackexchange site is lower and they get less attention due to the lack of critical mass of GAP developers and users there. $\endgroup$ Jun 3, 2020 at 21:10
  • 2
    $\begingroup$ As for the question, it is SortBy that does not return anything. Enter ?SortBy in GAP to see its documentation and examples. $\endgroup$ Jun 3, 2020 at 21:14
  • 1
    $\begingroup$ @EthanKowalenko great! See also gap-system.org/Manuals/doc/ref/chap12.html#X84545F3985C60F5B about identical objects - there are some subtle things there which can be easily encountered with your example. $\endgroup$ Jun 4, 2020 at 8:36

1 Answer 1


Just -- for future readers -- the reason for the initial problem:

SortBy changes a list (but does not return the sorted list), while e.g. Sorted would leave the list unchanged and return the sorted copy. The general language convention is that verbs do something to an object, while nouns create a new object with the desired characteristics.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .