• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. sort association table by values

Stats

  • Locked Locked
  • Replies 2
  • Subscribers 143
  • Views 15221
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

sort association table by values

Genas
Genas over 5 years ago

Hi,

I know you can sort association table by keys (keys=sort(myTable->? 'alphalessp) but i want to sort by values.

Because the values are numerical. For example i want the table below to be sorted numerically by the values.

MyTable["C"] = 5
MyTable["A"] = 5.5
MyTable["B"] = 3

should be 

MyTable["A"] = 5.5
MyTable["C"] = 5
MyTable["B"] = 3

Can anyone assist with the skill code to achieve this?

  • Cancel
Parents
  • Andrew Beckett
    Andrew Beckett over 5 years ago

    keys=mapcar('car sort(tableToList(MyTable) lambda((a b) cadr(a)>cadr(b))))

    (assuming you wanted them in decreasing numerical order, as you showed above - if you wanted them in increasing order, change the > to be a <).

    Breaking this down:

    • tableToList(MyTable) converts the table into an association list, i.e. (("B" 3) ("C" 5) ("A" 5.5))
    • The lambda is an anonymous function of two arguments, that compares the cadr (second element) of each list. So given ("B" 3) and ("A" 5.5) it would be comparing the 3 and 5.5
    • The sort returns the sorted association list
    • The final mapcar('car ...) applies the car function to each sublist and hence returns the first of these, i.e. the keys in the order you want.

    Andrew

    • Cancel
    • Vote Up +1 Vote Down
    • Cancel
Reply
  • Andrew Beckett
    Andrew Beckett over 5 years ago

    keys=mapcar('car sort(tableToList(MyTable) lambda((a b) cadr(a)>cadr(b))))

    (assuming you wanted them in decreasing numerical order, as you showed above - if you wanted them in increasing order, change the > to be a <).

    Breaking this down:

    • tableToList(MyTable) converts the table into an association list, i.e. (("B" 3) ("C" 5) ("A" 5.5))
    • The lambda is an anonymous function of two arguments, that compares the cadr (second element) of each list. So given ("B" 3) and ("A" 5.5) it would be comparing the 3 and 5.5
    • The sort returns the sorted association list
    • The final mapcar('car ...) applies the car function to each sublist and hence returns the first of these, i.e. the keys in the order you want.

    Andrew

    • Cancel
    • Vote Up +1 Vote Down
    • Cancel
Children
  • Genas
    Genas over 5 years ago in reply to Andrew Beckett

    Thanks, that solved my problem. I never knew there was a table to list function. 

    Thanks for the assistance

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information