Sorting alpha-numeric, string numeric values

Discussion in 'AutoCAD' started by RaghuMN, Dec 23, 2003.

  1. RaghuMN

    RaghuMN Guest

    Hi everybody,

    I have a list with the following words:
    ( "P16XYZ" "P20DGSgs" "P2GSDyh" "P1HHHye" "P11AXSss" "P45HHHhj" "P14AXCGs" "P3TYR")

    (acad_strlsort (list "P16XYZ" "P20DGSgs" "P2GSDyh" "P1HHHye" "P11AXSss" "P45HHHhj" "P14AXCGs" "P3TYR"))

    gives me the result as:
    ("P11AXSss" "P14AXCGs" "P16XYZ" "P1HHHye" "P20DGSgs" "P2GSDyh" "P3TYR" "P45HHHhj")

    In the above case, though there is "P1HHHye", "P2GSDyh", "P3TYR" and "P45HHHhj", these did not appear at the start of the sorted list.

    I wanted it to be this way:
    ("P1HHHye" "P2GSDyh" "P3TYR" "P45HHHhj" "P11AXSss" "P14AXCGs" "P16XYZ" "P20DGSgs")

    The numeric values are also sorted in the same way:
    Sorting of ("1" "3" "6" "22" "11" "111" "12") using acad_strlsort gives ("1" "11" "111" "12" "22" "3" "6").
    I wanted it to be ("1" "3" "6" "11" "12" "22" "111").

    Can any one help me to do some advanced sorting to get the ideal output that will have string numbers sorted according to their numeric value, where ever they are, either as string numbers or alpha-numeric characters.

    Thanks for any help.

    MNRaghu
     
    RaghuMN, Dec 23, 2003
    #1
  2. (acad_strlsort (list "001" "011" "111" "012" "022" "003" "006"))
    returns:
    ("001" "003" "006" "011" "012" "022" "111")

    Will this work for you?

    --
    Daniel J. Altamura, R.A.
    Altamura Architectural Consulting
    and SoftWorx, Autodesk Authorized Developer
    -----------------------------------------------------------------


    ("1" "11" "111" "12" "22" "3" "6").
     
    Daniel J. Altamura, R.A., Dec 23, 2003
    #2
  3. Take a look at these:

    (setq alist (list "P16XYZ" "P20DGSgs" "P2GSDyh" "P1HHHye" "P11AXSss"
    "P45HHHhj" "P14AXCGs" "P3TYR"))

    (setq order (vl-sort-i
    (mapcar (function (lambda (x)
    (atof (vl-list->string (cdr (vl-string->list x))))
    )
    )
    alist) '<))

    (foreach x order
    (setq nlist (cons (nth x alist) nlist))
    )


    (setq nlist (reverse nlist))


    --
    Ken Alexander
    Acad2000
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein

    "P11AXSss" "P45HHHhj" "P14AXCGs" "P3TYR"))
    and "P45HHHhj", these did not appear at the start of the sorted list.
    gives ("1" "11" "111" "12" "22" "3" "6").
    output that will have string numbers sorted according to their numeric
    value, where ever they are, either as string numbers or alpha-numeric
    characters.
     
    Ken Alexander, Dec 23, 2003
    #3
  4. MNRaghu,

    Your reply just now popped up in my outlook, only a few days late.
    Strange.

    As to your problem, I think you would want to separate the list
    according to the first character, sort them and then append the list
    back together in alphabectical order.
    --
    Ken Alexander
    Acad2000
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein

    not have the same letter. In such cases, this will not give a logical
    output.
    "P20DGSgs" "P45HHHhj" "Q1HHHye" "Q2GSDyh" "Q3TYR" "Q11AXSss"
    "Q14AXCGs" "Q16XYZ" "Q20DGSgs" "Q45HHHhj" )
    confusion there)
     
    Ken Alexander, Jan 7, 2004
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.