Sorting alpha-numeric, string numeric values

RaghuMN

Usenet Archive
Joined
Jul 14, 2003
Messages
97
Reaction score
0
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
 
(acad_strlsort (list "001" "011" "111" "012" "022" "003" "006")) returns:
("001" "003" "006" "011" "012" "022" "111")

Will this work for you?

RaghuMN said:
Sorting of ("1" "3" "6" "22" "11" "111" "12") using acad_strlsort gives
("1" "11" "111" "12" "22" "3" "6").
 
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))

RaghuMN said:
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.
 
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.
RaghuMN said:
Ken,

You have perfectly answered my question. Thanks a lot.

But, I should have been a little clear about my question.

Could you help me a little more in this aspect?

The case I gave you contained only one letter (P) from which the words started. Practically, the case would be any letter, and the whole lot will
not have the same letter. In such cases, this will not give a logical output.
If a case as below is taken (just copy the same set, replace P with Q) (setq alist (list "P16XYZ" "P20DGSgs" "P2GSDyh" "P1HHHye" "P11AXSss" "P45HHHhj" "P14AXCGs" "P3TYR" "Q16XYZ" "Q20DGSgs" "Q2GSDyh" "Q1HHHye" "Q11AXSss" "Q45HHHhj" "Q14AXCGs" "Q3TYR"))

using the your method, the output would be,

("Q1HHHye" "P1HHHye" "Q2GSDyh" "P2GSDyh" "Q3TYR" "P3TYR" "Q11AXSss" "P11AXSss" "Q14AXCGs" "P14AXCGs" "Q16XYZ" "P16XYZ" "Q20DGSgs" "P20DGSgs" "Q45HHHhj" "P45HHHhj")

whereas my expectation for the output type would be as below:

("P1HHHye" "P2GSDyh" "P3TYR" "P11AXSss" "P14AXCGs" "P16XYZ"
"P20DGSgs" "P45HHHhj" "Q1HHHye" "Q2GSDyh" "Q3TYR" "Q11AXSss" "Q14AXCGs" "Q16XYZ" "Q20DGSgs" "Q45HHHhj" )
Any further help please?

Thanks,

MNRaghu

(Daniel, Thanks, You are correct, it works for me, it was some
confusion there)
 

Latest threads

Back
Top