How to sort the value using ArraySort except zero data?

 

Hi, I want to sort the Array.

but  I want to sort except zero (0).

Is it possible with ArraySort code ?


 double Array[7]={0,3,0, 2,4,1,5};
  
  ArraySort(Array);

 

I want to get below.


Array[7] = {5,4,3,2,1}

 
Hong Ling Mu: Hi, I want to sort the Array. but  I want to sort except zero (0). Is it possible with ArraySort code ?
You have to create your own sorting function in that case.
 
Hong Ling Mu: I want to get below. Array[7] = {5,4,3,2,1}

Sort and then remove the zeros.

Not tested, not compiled, just typed.

#define INDEX uint
template<typename Ti, typename To>
INDEX      remove_copy(const Ti& inp[], INDEX iBeg, INDEX iEnd,
                             To& out[], INDEX oBeg, const Ti& value){
   for(; iBeg != iEnd; ++iBeg){
      if(!(inp[iBeg] == value) )    out[oBeg++] = inp[iBeg];
   }
   return oBeg;
}
//////////////////////////////////////////////////////////
  double Array[]={0,3,0, 2,4,1,5};
  ArraySort(Array);
  // Array = {0,0,1,2,3,4,5}
  ArrayResize(Array, remove_copy(Array, 0, ArraySize(Array), Array, 0, 0) );
  // Array = {1,2,3,4,5}
  ArrayGetAsSeries(Array, !ArrayGetAsSeries(Array) );
  // Array = {5,4,3,2,1}

Not tested, not compiled, just typed.

 
Fernando Carreiro #:
You have to create your own sorting function in that case.

Thank you.

I wish we could have a simple code,,, but not available..

Grazie!

 
William Roeder #:

Sort and then remove the zeros.

Not tested, not compiled, just typed.

Not tested, not compiled, just typed.

Thank you for the new code.

Let me try and feed back!