Sort and rsort with clause application

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 ?

The with() clause for sort() and rsort() is not a condition for selection–it is a expression used for comparison while sorting.

Lets say you have an array of rectangles.

typedef struct {int length, width;} rect_t;
rect_t array_rect[];

To sort the array by area:

array_rect.sort(r) with (r.length * r.width);

Thanks Dave for the explanation. So the example you provided it will the sort the array according to the area.

But still for my example I donot get how the below output is getting generated. Can you help explain in understanding the below output?

int intA[8] = '{3,2,1,6,8,7,4,9};
intA.sort (x) with (x > 3);

Output -

AFTER ‘intA’ sort: intA=’{2, 1, 3, 4, 7, 9, 6, 8}

Think of the with() clause of creating another array iterating over each element. That becomes an array of single bits because the result of (x >3) is a single bit.

int  intA[8] = '{3,2,1,6,8,7,4,9};
bit withA[8] = '{0,0,0,1,1,1,1,1};

sort() uses that array when performing the sort, but there are only two values; 0 and 1. (It just so happens that the first 3 elements are 0 and remaining elements are 1). Since 0 has the least magnitude for the sorting order can choose any of the three elements first in any order. 1 is the next level and those elements will be chosen in any order.

I suggest trying other expressions like (x % 2) to make sure you get a better understanding.