• 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. simple sort question

Stats

  • Locked Locked
  • Replies 6
  • Subscribers 143
  • Views 15260
  • 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

simple sort question

nosaj
nosaj over 13 years ago

I'm trying to sort a list of strings with sort alphalessp, can anyone explain why "l" in the list is dropped in the following example?

How may I sort the list without losing elments AND changing the original list?

I tried saving list a to a tmp list, that doesn't seem to help.  

Thanks in advance!

Commands :

a=list("res" "sumW" "w" "sumL" "l" "rs" "segSp" "srs")
length(a)
sort(a 'alphalessp)
length(a)
a

CIW Output :

a=list("res" "sumW" "w" "sumL" "l" "rs" "segSp" "srs")
("res" "sumW" "w" "sumL" "l"
    "rs" "segSp" "srs"
)
length(a)
8
sort(a 'alphalessp)
("l" "res" "rs" "segSp" "srs"
    "sumL" "sumW" "w"
)
length(a)
7
a
("res" "rs" "segSp" "srs" "sumL"
    "sumW" "w"
)

  • Cancel
  • dmay
    dmay over 13 years ago

    Sorts are destructive operations on the list. The head of the list changes. Do the following:

    a = sort(a 'alphalessp)

    Derek

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • skillUser
    skillUser over 13 years ago

     Hi,

    Yes, this is because "sort" is a destructive list operator, so 'a' is modified in place, to be safe, assign the return value of sort back to the variable:

     a = sort(a 'alphalessp)

     You can assign the variable contents to another variable before sorting, if you don't want to change 'a':

     b = copy(a)
     b = sort(b 'alphalessp)

     Or you could write your own sorting algorithm!

    Hope this helps.

    Regards,

    Lawrence.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • nosaj
    nosaj over 13 years ago

    Thanks.  But why did sort drop "l" in this example and not other strings?

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • skillUser
    skillUser over 13 years ago

    I cannot say specifically why "l" and not other strings since that would require detailed knowledge of the implementation of the sort algorithm (which I do not have), but it is presumably more efficient to implement it as a destructive operation, which is why things may go missing because the 'head' of the list changes, as Derek pointed out (didn't see his append until I'd posted mine).

    Regards,

    Lawrence.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 13 years ago

    The sort() function is not dropping anything. You are supposed to use the return value of sort(), and not depend upon the side effects on the variable passed in.

    It's actually easy enough to understand what is happening without understanding the sort algorithm. The variable a points to a list cell containing the value "res" and the pointer for that list cell points to another list cell containing the value "sumW" and so on (before the sort).

    The sort will re-use the existing list cells, and just destructively modify the pointers - this avoids re-allocating any memory. So after the sort, the list cell containing "res" will point to the list cell containing "rs" (the next in the sorted sequence). In addition, the list cell containing "l" will point to the list cell containing "res" and the return value of the sort will be a pointer to the list cell containing "l" as that's the first in the list.

    The variable a is still pointing at the list cell containing "res" - the sort cannot change that pointer because it has no means of knowing what variables are pointing to the original list.

    So the variable a is pointing part way along the sorted list.

    What will happen is that when you call sort and do not re-assign the variable to the return value, the car() of the variable passed in will remain the same before and after the sort. You'll simply get the remainder of the sorted list from that point onwards. Since "res" is the second entry in the sorted list, you just get the list from that point onwards.

    Since this is a side effect, the only sensible thing to do is use the return value of the function, as is described in the documentation - which also indicates that sort() is a destructive function.

    Hope that clarifies things?

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • nosaj
    nosaj over 13 years ago

    thank you all for your reply.

    • 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