«

»

Nov 17

Sorting Associative Array by contents in SystemVerilog

During our ongoing SystemVerilog training at CVC (www.cvcblr.com) a smart attendee asked what happens when I sort an Associative Array. While PHP and few other language allow this through built-in functions, SV doesn’t allow it, given the complexities of ordering with respect to keys, non sortable values etc. Here is a quick example of how this can be worked around in simple int associative array (assuming we need value based sorting and maintaining the keys).

 

Nice example for a training indeed!

 

SV_AA

1 comment

  1. Tudor Timi

    This doesn’t behave the same as PHP’s asort() function. I’m not sure what you see on your simulator, but on EDAPlayground’s Aldec Riviera-PRO I get the following:

    For the original array: {2:5678, 100:444, 200:333, 300:1111}
    For the “sorted” array: {2:333, 100:444, 200:1111, 300:5678}

    The two arrays are completely different, whereas asort() in PHP does a sort without modifying the key-val associations. Sorting like this isn’t possible in SV, as an associative array is stored in the order of the keys.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>