Log In  


I have a table:
a={0,0,1,2,0,6,9,2,0,5,3,0,7,0,0}
I want to sort it so the 0's are moved to the front
This works:

 a={0,0,1,2,0,3,4,0,0,5,6,7,8,0,9,10}
 b={}
 c={}
 for v in all(a) do
  if v==0 then
   add(b,v)
  else
   add(c,v)
  end
 end
 for v in all(c) do
 add(b,v)
 end
 a=b

but is clunky and since this will be in a recursive function, efficiency is essential.
what is a more elegant/quicker way to do this?



This isn't a direct answer, but is this going to be generically available and used for a multitude of situations or is it designed for a very narrow scope? This question should determine the direction of your answer, I think.


generically available and used for a multitude of situations.

the example i gave is not the actual code. it will not be numbers and zeroes,
but rather multidimensional arrays. I'll need to move all the internal arrays that have a certain value to the front. So i'm hoping for an answer that deals with the concept, not just the above example.
the real thing is more like:

for m in all(moves) do
 if m.cap!=0 then
  add(b,m)
 else
  add(c,m)
 end
end
-- ..etc ..etc


[Please log in to post a comment]