• 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. Allegro X PCB Editor
  3. Sorting with secondary parameters in assoc list

Stats

  • Replies 3
  • Subscribers 160
  • Views 14577
  • Members are here 0
More Content

Sorting with secondary parameters in assoc list

SkilSiao
SkilSiao over 14 years ago

Hi, I have a list looks like this...

mylist='((20 3.5) (15 2.5) (50 3.1) (20 2.8) (5 3.6) (20 3.6) (20 0.1))

if I apply sortcar with 'lessp, it becomes:

Skill >  sortcar(mylist 'lessp)
((5 3.6) (15 2.5) (20 0.1) (20 3.6)  (20 2.8) (20 3.5) (50 3.1))

i want to sort with respect to the 2nd element as well in the assoc list.

that is to have ( (5 3.6) (15 2.5) (20 0.1) (20 2.8) (20 3.5) (20 3.6) (50 3.1) ).

 is there a way to go with it?

 Thanks in advance.

  • Cancel
  • Sign in to reply
  • aCraig
    aCraig over 14 years ago

    I don't believe there is a multi column sort. One thougt would be: after you do the sortcar you'd need to iterate over the list and create an array or table of sublists based on the "car". Then modify the sublist so the elements are '(b '(a b), then you can sortcar on the sublist. Once all the sublists are sorted you'll need to put them back to '(a b) and then rebuild the original list.

    Just a thought

    Craig

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • RFDTeradyne
    RFDTeradyne over 14 years ago

    Use "sort" which allows you to specify a sort function. It is passed two arguments and should return non-nil if a<b and nil if a>= b  Create one that does something like:

     (defun mysortfunc (a b)

     (if  (equal (car a) (car b) )  ( return (lessp (cadr a) (cadr b) ) )  

     (return (lessp (car a) (car b) ) )

    )

     I am doing this from memory and may not have gotten the syntax exactly right.  It the sort is backwards, replace lessp with greaterp.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • SkilSiao
    SkilSiao over 14 years ago

    Hi RFDTeradyne,

    Yes, the function works well. Thanks.

    Define the sorting algor as:

    procedure( mySortFunc(a b)
    prog( ()
    if( eq(car(a) car(b)) then
     return(cadr(a)<cadr(b)) else
     return(car(a)<car(b))
    ) ; end if
    ) ; end prog
    ) ; end proc mySortFunc

    and try:

    Skill> sort( list('(7 45) '(7 56) '(5 23) '(24 13) '(25 1) '(6 2) '(7 5) '(24 8)) 'mySortFunc)

    -> ((5 23)  (6 2) (7 5) (7 45) (7 56) (24 8) (24 13) (25 1))

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Cadence Guidelines

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