• 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. How to sort list of lists?

Stats

  • Locked Locked
  • Replies 8
  • Subscribers 144
  • Views 9172
  • 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

How to sort list of lists?

Sheppie
Sheppie over 2 years ago

Hello.

In order to automatically number devices, I have written code to extract all relevant data from a cell-view and store it in a list of lists. The list of lists looks something  like this:

list( '(23.5 nameA cellId) '(18.3 nameB cellId) '(73.0 nameC cellId) '(23.5 nameD cellId) '(44.9 nameE cellId))

where every cellId is unique, and the first value may appear multiple times (like the 23.5 value), in that case the order has to follow the name (names are unique)

In order to give each cell the proper number, I have to order this list, the first element defines the order, then the name. It should look like this:

list( '(18.3 nameB cellId) '(23.5 NameA cellId) '(23.5 nameD cellId) '(44.9 nameE cellId) '(73.0 nameC cellId))

Sorting a simple list of values is easy. I can think of complicated code to sort my list, however, I can imagine that there is a way simpler method of doing it.

Does anyone have a idea how I could efficiently sort my list?

If this is rather complicated, I think I can make the rest of my code to work with only sorting defined by the first element, so ignoring the name.

Thanks in advance.

With kind regards,

Sjoerd

  • Cancel
  • mschw
    mschw over 2 years ago

    Dear Sjord,

    you can try that

    (sort (list '(23.5 nameA cellId) '(18.3 nameB cellId) '(73.0 nameC cellId) '(23.5 nameD cellId) '(44.9 nameE cellId)) (lambda (x y) (lessp (car x) (car y))))

    Regards

    Matthias

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • AurelBuche
    AurelBuche over 2 years ago

    Hi,

    If you want to sort the list by comparing the first elements, you can use the following command :

    (sortcar your_list 'lessp)

    Otherwise you just have to define a custom comparison function :

    (sort your_list
      (lambda (l0 l1)
        ;; Compare l0 and l1 by first element, or by name when they are equal
        (destructuringBind (n0 name0 cell0) l0
          (destructuringBind (n1 name1 cell1) l1
            (if (equal n0 n1) (alphalessp name0 name1) (lessp n0 n1))
            ))))

    Cheers,

    Aurel

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Sheppie
    Sheppie over 2 years ago in reply to mschw

    Hi Matthias,

    Thanks for the quick response.

    Your solution kind-of works. It perfectly sorts on the first element (the real), but doesn't look at the name part.

    I did this:

    myList = list( '(23.5 nameA cellId) '(18.3 nameB cellId) '(73.0 nameC cellId) '(23.5 nameD cellId) '(44.9 nameE cellId))
    sort( myList (lambda (x y) lessp( car(x) car(y))))

    And this is what the output was:

    ((18.3 nameB cellId)
        (23.5 nameD cellId)
        (23.5 nameA cellId)
        (44.9 nameE cellId)
        (73.0 nameC cellId)
    )

    As you can see, the order is correct for the first element, however, the 23.5 lists should've been the other way around:

    ((18.3 nameB cellId)
        (23.5 nameA cellId)
        (23.5 nameD cellId)
        (44.9 nameE cellId)
        (73.0 nameC cellId)
    )

    My understanding is that your solution only sorts on the first element, and doesn't take the second element into account, am I right?

    Thanks for your support.

    With kind regards,

    Sjoerd

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 2 years ago in reply to Sheppie

    Sjoerd,

    Take a look at Aurélien's later post which gives a sort function that uses the name as a tiebreaker.

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Sheppie
    Sheppie over 2 years ago in reply to AurelBuche

    Hi Aurel,

    Thank you for you quick response. I've tried your code and it works! It does what I'd like it to do.

    I'll definitely have to spend time to understand your code (never seen the destructuringBind procedure...) and learning something, so thank you for sharing this.

    With kind regards,

    Sjoerd

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • mschw
    mschw over 2 years ago in reply to Sheppie

    Dear Sjord,

    sorry, I did not spend enough time on reading your post.

    I hope that is useful

    (sort (list '(23.5 nameA cellId) '(18.3 nameB cellId) '(73.0 nameC cellId) '(23.5 nameD cellId) '(44.9 nameE cellId)) (lambda (x y) (if (equal (car x) (car y)) (alphalessp (cadr x) (cadr y)) (lessp (car x) (car y)))))

    Regards

    Matthias

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Sheppie
    Sheppie over 2 years ago in reply to Andrew Beckett

    Hi Andrew,

    I've tested both solutions and the solution of Aurélien is the one I went with. Learned something new. Looking at his solution, I realized that some custom procedures I wrote can be rewritten and made way more efficient, So during the summer holiday period I'll use the less-busy times to go over my code library (=library of design team) and update where possible.

    With kind regards,

    Sjoerd

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Sheppie
    Sheppie over 2 years ago in reply to mschw

    Hi Matthias,

    Thank you for your update. Your solution looks very similar to the solution of Aurélien. Since I got the my code using the solution of Aurélien working, I'll stick to that. But I'll definitely remember your solution for the future.

    Many thanks.

    With kind regards,

    Sjoerd

    • 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