# Function for SortBy

Let's say I have the following list

list = {{-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 1, 0, 0}, {0, -1, 0, 1},
{0, -1, 1, 0}, {0, 0, -1, 1}, {0, 0, 1, -1}, {0, 1, -1, 0},
{0, 1, 0, -1}, {1, -1, 0, 0}, {1, 0, -1, 0}, {1, 0, 0, -1}}


What sort function (sfunc) used in SortBy [list, sfunc] can give me slist?

slist = {{0, 0, 1, -1}, {0, 0, -1, 1}, {0, -1, 0, 1}, {-1, 0, 0, 1},
{0, 1, 0, -1}, {0, 1, -1, 0}, {0, -1, 1, 0}, {-1, 0, 1, 0},
{1, 0, 0, -1}, {1, 0, -1, 0}, {1, -1, 0, 0}, {-1, 1, 0, 0}}


Few examples of sorted data

slist1 =  {{0, 0, 1, -2}, {0, 0, -2, 1}, {0, -2, 0, 1}, {-2, 0, 0, 1}, {0, 1, 0, -2}, {0, 1, -2, 0}, {0, -2, 1, 0}, {-2, 0, 1, 0}, {1, 0, 0, -2}, {1, 0, -2, 0}, {1, -2, 0, 0}, {-2, 1, 0, 0}, {0, 1, -1, -1}, {0, -1, 1, -1}, {0, -1, -1, 1}, {-1, 0, 1, -1}, {-1, 0, -1, 1},{-1, -1, 0, 1}, {1, 0, -1, -1}, {1, -1, 0, -1}, {1, -1, -1, 0}, {-1, 1, 0, -1}, {-1, 1, -1, 0}, {-1, -1, 1, 0}}

slist2 = {{0, 0, 2, -2}, {0, 0, -2, 2}, {0, -2, 0, 2}, {-2, 0, 0, 2}, {0, 1, 1, -2}, {0, 1, -2, 1}, {0, -2, 1, 1}, {-2, 0, 1, 1}, {0, 2, 0, -2}, {0, 2, -2, 0}, {0, -2, 2, 0}, {-2, 0, 2, 0}, {1, 0, 1, -2}, {1, 0, -2, 1}, {1, -2, 0, 1}, {-2, 1, 0, 1}, {1, 1, 0, -2}, {1, 1, -2, 0}, {1, -2, 1, 0}, {-2, 1, 1, 0}, {2, 0, 0, -2}, {2, 0, -2, 0}, {2, -2, 0, 0}, {-2, 2, 0, 0}, {0, 2, -1, -1}, {0, -1, 2, -1}, {0, -1, -1, 2}, {-1, 0, 2, -1}, {-1, 0, -1, 2}, {-1, -1, 0, 2}, {1, 1, -1, -1}, {1, -1, 1, -1}, {1, -1, -1, 1}, {-1, 1, 1, -1}, {-1, 1, -1, 1}, {-1, -1, 1, 1}, {2, 0, -1, -1}, {2, -1, 0, -1}, {2, -1, -1, 0}, {-1, 2, 0, -1}, {-1, 2, -1, 0}, {-1, -1, 2, 0}}

• Explain the core idea behind your sorted list. It certainly isn't clear what you're seeking. Commented Jan 4, 2019 at 19:01
• Can you give some examples with more complicated data? Commented Jan 4, 2019 at 19:33
• @MikeY I have added 2 more sorted sets. Commented Jan 4, 2019 at 20:10
• So if you sort your data like this, it can probably be counted and therefore indexed in closed form. Commented Jan 5, 2019 at 20:05
• @MikeY Yes I have decided to use this sorting. I have edited by bounty question. If you have time then you could answer that, then I can gladly give you the bounty. Commented Jan 6, 2019 at 3:13

EDITED TO ADD A SORT CRITERION
For the data sets, you are sorting on

1. the number of negative numbers first, then
2. the subset of just the nonnegative elements (using canonical ordering for lists), then
3. the set gained when you replace negative terms with a '1' and nonnegative with '0'
4. The subset of just the negative elements (using canonical ordering)

 funkySort[list_]:= SortBy[list,{
Count[#, _?Negative] &,
Select[#, NonNegative] &,
Negative,
Select[#, Negative] &
}]

slist == funkySort[slist[[RandomPermutation[Length[slist]]]]]
slist1 == funkySort[slist1[[RandomPermutation[Length[slist1]]]]]
slist2 == funkySort[slist2[[RandomPermutation[Length[slist2]]]]]


True

True

True

• But slist != funkySort[list] . Are you sure this works. It doesn't seem to work on my system. Do you get really get slist1 and slist2 when your function is applied to any random ordering of those lists. Commented Jan 5, 2019 at 14:25
• Oops, copied over the wrong funkySort[ ] from my notebook. Fixed it... Commented Jan 5, 2019 at 17:00