8
$\begingroup$

One can specify the second argument of SortBy not to sub-sort ties, i.e. cases that have identical values according to the main sorting function. SortBy[list, {First}] ensures that sublists are sorted by the first (real) element, and ties are not resolved but left in the order they appear in the original list.

list = {{.1, {0, 1, 2}}, {.4, {1}}, {.1, {0, 1}}, {.7, {0}}};

Column /@ {list, SortBy[list, First], SortBy[list, {First}], SortBy[list, {}], SortBy[list, Identity]}

enter image description here

How can I specify a second argument for SortBy that leaves list as is, without actually sorting it? Clearly, {} or Identity, {Identity}, etc. do not achieve this.

While I strongly believe this cannot be done, as SortBy is a function specifically designed to sort, I nevertheless post this as there may be some undocumented way to do it simply.

$\endgroup$

2 Answers 2

9
$\begingroup$

I would have expected SortBy[list,{}] to also work, but at least the following seems to do the trick:

SortBy[list, {0 &}] === list
(* True *)

This is effectively using the "stable-sort" property of SortBy[...,{func}] with a "useless" sorting function that always returns the same value (0 in this case)

$\endgroup$
3
  • 1
    $\begingroup$ This is surprising. Where do you learn about the "stable-sort" property? This doesn't seem to be mentioned in document of SortBy. $\endgroup$
    – xzczd
    Nov 30, 2022 at 8:38
  • $\begingroup$ @xzczd I can't recall how I first learned about it, but it's mentioned a few times on this site, e.g. here it is explicitly called out. I think I also saw a question about being documented or not, but I can't find it now... $\endgroup$
    – Lukas Lang
    Nov 30, 2022 at 8:50
  • $\begingroup$ I also assumed that sorting is stable, and I am surprised that I cannot find this in the docs. $\endgroup$
    – Szabolcs
    Nov 30, 2022 at 9:23
5
$\begingroup$

How about:

i = 0; SortBy[list, i++&]

Or skip SortBy with upvalue:

SortBy[l_, nosort] ^:= l
SortBy[list, nosort]
$\endgroup$
3
  • $\begingroup$ These are nice, but in my case I need a solution that only involves the 2nd argument of SortBy. For context: I define sorting as an option to a higher level function that is internally passed on as the 2nd arg to SortBy, without modification. I rather not have users specify contrived sorting functions by meddling with either global variables or upvalues just to pass it on as an option. $\endgroup$ Nov 30, 2022 at 11:45
  • $\begingroup$ @IstvánZachar How about including the nosort as a symbol in the package? (I guess it's a package?) $\endgroup$
    – xzczd
    Nov 30, 2022 at 13:46
  • $\begingroup$ That could work, but then again the user has to remember another custom symbol (I already have 300+). For the case at hand, Lukas's answer is exactly what I was looking for. $\endgroup$ Dec 1, 2022 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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