How to sort only the first k elements of an array using sort method?

How to sort only the first k elemets of array using sort method?

In reply to Manoj J:


module tb();
  int q[$]={32'd11,32'd2,32'd13,32'd4,32'd15,32'd6,32'd17,32'd8,32'd19,32'd10};
  int a[$];
  int k;

initial begin
  k = 5;
  q.shuffle();
  for(int i=0;i<k;i++) a[i] = q[i];
  foreach(a[ii]) $display("before a[%0d] is : ",ii,a[ii]);
  a.sort();
  foreach(a[ii]) $display("after a[%0d] is : ",ii,a[ii]);
end
endmodule

In reply to karan_18:

Thanks karan for the answer.
I have one question can we do like this using sort method??


q.sort with (item.index > 5);

Such that it only sort the elements of the of the array from index > 5.

In reply to Manoj J:

q.sort with (item.index > 5);

This will just separate the elements from 0 to 5 and 6 to $ into 2 groups, but it will not sort.

In reply to karan_18:

Thanks for the clarification.