Tutorials

Learn More

Array ordering methods in SV

The ordering methods are used to reorder the single-dimensional arrays or queues.

Methods

Description

shuffle

Randomizes the order of the elements in an array

sort

Sorts the unpacked array in ascending order

rsort

Sorts the unpacked array in descending order

reverse

Reverses all the elements of a packed or unpacked array.

Note:

  1. The sort and rsort methods may use the ‘with’ clause. The ‘with’ clause usage is optional. 
  2. The shuffle and reverse methods lead to a compilation error if the ‘with’ clause is specified.

Array ordering methods Example

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

module arr_ordering_ex;
  int arr[8] = '{5,6,9,2,3,4,6,10};
  int que[$];
  initial begin
    que.push_back(45);
    que.push_back(30);
    que.push_back(99);
    que.push_back(51);
    que.push_back(85);
    
    $display("-------------------------------------");
    $display("------ Printing array and queue -----");
    $display("-------------------------------------");
    print_array_queue();
    
    $display("-------------------------------------");
    $display("----------- shuffle method ----------");
    $display("-------------------------------------");
    shuffle_method();
    print_array_queue();
    
    $display("-------------------------------------");
    $display("---------- reverse method -----------");
    $display("-------------------------------------");
    reverse_method();
    print_array_queue();
    
    $display("-------------------------------------");
    $display("------------- sort method -----------");
    $display("-------------------------------------");
    sort_method();
    print_array_queue();
    
    $display("-------------------------------------");
    $display("------------ rsort method -----------");
    $display("-------------------------------------");
    rsort_method();
    print_array_queue();
  end
  
  //-------------------------
  // Array gen and methods
  //-------------------------
  
  function void print_array_queue();
    $display("Array = %p", arr);
    $display("Queue = %p", que);
  endfunction
  
  function void shuffle_method();
    arr.shuffle();
    que.shuffle();
  endfunction
  
  function void sort_method();
    arr.sort();
    que.sort();
  endfunction
  
  function void rsort_method();
    arr.rsort();
    que.rsort();
  endfunction
  
  function void reverse_method();
    arr.reverse();
    que.reverse();
  endfunction
endmodule

Output:

-------------------------------------
------ Printing array and queue -----
-------------------------------------
Array = '{5, 6, 9, 2, 3, 4, 6, 10}
Queue = '{45, 30, 99, 51, 85}
-------------------------------------
----------- shuffle method ----------
-------------------------------------
Array = '{10, 3, 9, 2, 5, 6, 4, 6}
Queue = '{45, 30, 51, 85, 99}
-------------------------------------
---------- reverse method -----------
-------------------------------------
Array = '{6, 4, 6, 5, 2, 9, 3, 10}
Queue = '{99, 85, 51, 30, 45}
-------------------------------------
------------- sort method -----------
-------------------------------------
Array = '{2, 3, 4, 5, 6, 6, 9, 10}
Queue = '{30, 45, 51, 85, 99}
-------------------------------------
------------ rsort method -----------
-------------------------------------
Array = '{10, 9, 6, 6, 5, 4, 3, 2}
Queue = '{99, 85, 51, 45, 30}

System Verilog Tutorials