module arrayOrder;
string str[5] = '{"bob", "george", "ringo", "john",
"paul"};
int intA[8] = '{3,2,1,6,8,7,4,9};
initial begin
$display("BEFORE 'str' reverse: str=%p", str);
//str.reverse (x) with (x > 5);
//Compile ERROR - can't use 'with' clause
str.reverse;
$display("AFTER 'str' reverse: str=%p", str);
$display("BEFORE 'intA' sort: intA=%p", intA);
intA.sort (x) with (x > 3); //OK – 'with' clause is ok
//intA.sort;
$display("AFTER 'intA' sort: intA=%p",intA);
$display("BEFORE 'intA' rsort: intA=%p",intA);
intA.rsort (x) with (x > 3);
//intA.rsort;
$display("AFTER 'intA' rsort: intA=%p",intA);
$display("BEFORE 'intA' shuffle: intA=%p",intA);
//intA.shuffle (x) with (x < 5); //Compile ERROR –
//cannot use 'with' clause
intA.shuffle;
$display("AFTER 'intA' shuffle: intA=%p",intA);
end
endmodule
Output -
BEFORE ‘str’ reverse: str=‘{“bob”, “george”, “ringo”, “john”, “paul”}
AFTER ‘str’ reverse: str=’{“paul”, “john”, “ringo”, “george”, “bob”}
BEFORE ‘intA’ sort: intA=‘{3, 2, 1, 6, 8, 7, 4, 9}
AFTER ‘intA’ sort: intA=’{2, 1, 3, 4, 7, 9, 6, 8}
BEFORE ‘intA’ rsort: intA=‘{2, 1, 3, 4, 7, 9, 6, 8}
AFTER ‘intA’ rsort: intA=’{9, 7, 8, 6, 4, 1, 2, 3}
BEFORE ‘intA’ shuffle: intA=‘{9, 7, 8, 6, 4, 1, 2, 3}
AFTER ‘intA’ shuffle: intA=’{2, 4, 3, 6, 8, 1, 9, 7}
In the above code, I used a with clause with sort and rsort instead of using the simple sort and rsort. I was expecting that the code will sort items which are greater than 3. But the output does not seem to be like that. Can someone help understand how system verilog sort and rsort is sorting and rsorting with the with clause ?