2
$\begingroup$

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}}
$\endgroup$
5
  • 4
    $\begingroup$ Explain the core idea behind your sorted list. It certainly isn't clear what you're seeking. $\endgroup$ Commented Jan 4, 2019 at 19:01
  • 2
    $\begingroup$ Can you give some examples with more complicated data? $\endgroup$
    – MikeY
    Commented Jan 4, 2019 at 19:33
  • 1
    $\begingroup$ @MikeY I have added 2 more sorted sets. $\endgroup$
    – Hubble07
    Commented Jan 4, 2019 at 20:10
  • $\begingroup$ So if you sort your data like this, it can probably be counted and therefore indexed in closed form. $\endgroup$
    – MikeY
    Commented Jan 5, 2019 at 20:05
  • $\begingroup$ @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. $\endgroup$
    – Hubble07
    Commented Jan 6, 2019 at 3:13

1 Answer 1

9
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Hubble07
    Commented Jan 5, 2019 at 14:25
  • $\begingroup$ Oops, copied over the wrong funkySort[ ] from my notebook. Fixed it... $\endgroup$
    – MikeY
    Commented Jan 5, 2019 at 17:00

Your Answer

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

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