Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort array of integers with zeros at the end?

I need to sort arrays by integer field, with 1-n sorted at the beginning and zeros last: 0,0,3,1,2 -> 1,2,3,0,0

I don't know how to sort it in one go, so I tried in 2 sorts, but it doesn't produce correct results:

It does put zeros at the end, but it messes up 1-n ordered items:

0,0,3,1,2 -> (first sort) 0,0,1,2,3 -> (second sort) 2,3,1,0,0

procedure TForm2.Button1Click(Sender: TObject);
var
  Arr: TArray<integer>;
begin
  SetLength(Arr, 5);
  Arr[0] := 0;
  Arr[1] := 0;
  Arr[2] := 3;
  Arr[3] := 1;
  Arr[4] := 2;

  // First sort: Sort 1-n
  TArray.Sort<integer>(Arr, TComparer<integer>.Construct(function(const Left, Right: integer): Integer
    begin
      if Left < Right then
        Result := -1
      else if Left > Right then
        Result := 1
      else
        Result := 0;
    end
    ));

  // Second sort: Put zeros at the end
  TArray.Sort<integer>(Arr, TComparer<integer>.Construct(function(const Left, Right: integer): Integer
    begin
      if (Left = 0) and (right>0) then
        Result := 1
      else
        Result := 0;
    end
    ));
end;

Is there a way to do this kind of sort in one, single Sort operation?

like image 879
Mike Torrettinni Avatar asked Jan 06 '18 16:01

Mike Torrettinni


2 Answers

Try this, the point being to deal with the special 0 cases first in the if-then-else ladder, before the ordinary cases.

  TArray.Sort<integer>(Arr, TComparer<integer>.Construct(function(const Left, Right: integer): Integer
    begin
    if (Left = 0) and (Right = 0) then
      Result := 0
    else if (Left = 0) then
      Result := 1
    else if (Right = 0) then
      Result := -1
    else if (Left < Right) then
      Result := -1
    else if (Left > Right) then
      Result := 1
    else
      Result := 0;
    end
    ));

A brief testing shows it works OK.

like image 64
Tom Brunberg Avatar answered Sep 26 '22 14:09

Tom Brunberg


Just fix your compare function so that it will treat 0 as being larger than anything.

Untested:

TArray.Sort<integer>(Arr, TComparer<integer>.Construct(function(const Left, Right: integer): Integer
  begin
    if Left = Right then
      Result := 0
    else if ((Left <> 0) and (Left < Right)) or (Right = 0) then
      Result := -1
    else 
      Result := 1;
  end
  ));
like image 21
gabr Avatar answered Sep 25 '22 14:09

gabr