Hi,
I am having a problem rearranging a list into a specified order and
wondered if anyone could help? I want to sort the list by the 9th
character (e.g. F in 'CEI2110.FC2) by a preset order I, R, C, F
('CEI2110.IL2 'CEI2110.IL1 'CEI2110.FC2 'CEI2110.FC1 'CEI1001.R
'CEi4070.C)
=> ('CEI2110.IL1 'CEI2110.IL2 'CEI1001.R 'CEI4070.C 'CEI2110.FC1
'CEI2110.FC2)
Thanks
Mike
"Mike Weston" <················@bnfl.com> wrote in message
·································@posting.google.com...
> Hi,
>
> I am having a problem rearranging a list into a specified order and
> wondered if anyone could help? I want to sort the list by the 9th
> character (e.g. F in 'CEI2110.FC2) by a preset order I, R, C, F
What have you got so far?
Coby
(remove #\space "coby . beck @ opentgi . com")
················@bnfl.com (Mike Weston) writes:
> Hi,
>
> I am having a problem rearranging a list into a specified order and
> wondered if anyone could help? I want to sort the list by the 9th
> character (e.g. F in 'CEI2110.FC2) by a preset order I, R, C, F
This can't be a homework problem, unless you are doign a weird course!
If the lists are really like this:
(CEI2110.IL2 CEI2110.IL1 CEI2110.FC2 CEI2110.FC1 CEI1001.R
CEi4070.C)
(no quotes in there), then something like this might help.
(defun obscuro-sort (l &key
(index 8)
(coding '((#\I . 0)
(#\R . 1)
(#\C . 2)
(#\F . 3))))
;; destructive of L, probably
;; L is a list of symbols with names with at least index+1 chars.
(sort l #'< :key #'(lambda (s)
(let ((c (aref (symbol-name s) index)))
(or (cdr (assoc c coding))
;; uncoded things wil sort at the start.
-1)))))
--tim
················@bnfl.com (Mike Weston) writes:
> Hi,
>
> I am having a problem rearranging a list into a specified order and
> wondered if anyone could help? I want to sort the list by the 9th
> character (e.g. F in 'CEI2110.FC2) by a preset order I, R, C, F
>
> ('CEI2110.IL2 'CEI2110.IL1 'CEI2110.FC2 'CEI2110.FC1 'CEI1001.R
> 'CEi4070.C)
>
> => ('CEI2110.IL1 'CEI2110.IL2 'CEI1001.R 'CEI4070.C 'CEI2110.FC1
> 'CEI2110.FC2)
>
> Thanks
>
> Mike
The following appears to work, but (probably) needs to be made
more robust.
* (let ((order #(#\I #\R #\C #\F)))
(defun mw-< (a b)
(labels ((key (x)
(char (if (symbolp x) (symbol-name x) x) 8))
(rank (x)
(position (key x) order)))
(< (rank a) (rank b)))))
; converted MW-<
MW-<
* (sort '(CEI2110.IL2 CEI2110.IL1 CEI2110.FC2 CEI2110.FC1 CEI1001.R CEi4070.C)
#'mw-<)
(CEI2110.IL2 CEI2110.IL1 CEI1001.R CEI4070.C CEI2110.FC2 CEI2110.FC1)
*
--
Raymond Wiker
·············@fast.no
From: Friedrich Dominicus
Subject: Re: Rearranging lists in lisp
Date:
Message-ID: <87elsfciqu.fsf@frown.here>
················@bnfl.com (Mike Weston) writes:
> Hi,
>
> I am having a problem rearranging a list into a specified order and
> wondered if anyone could help? I want to sort the list by the 9th
> character (e.g. F in 'CEI2110.FC2) by a preset order I, R, C, F
>
> ('CEI2110.IL2 'CEI2110.IL1 'CEI2110.FC2 'CEI2110.FC1 'CEI1001.R
> 'CEi4070.C)
>
> => ('CEI2110.IL1 'CEI2110.IL2 'CEI1001.R 'CEI4070.C 'CEI2110.FC1
> 'CEI2110.FC2)
check out sort
sligtly modified list
(setq test-list '("CEI2110.IL2" "CEI2110.IL1" "CEI2110.FC2"
"CEI2110.FC1" "CEI1001.R" "CEI4070.C"))
(have used Strings instead of symbols but even with symbols one can do
that)
(defun my-rearrange (list-of-strings)
(flet ((cmp-func (ch1 ch2)
(let ((in-list '(#\I #\R #\C #\F)))
(assert (and (member ch1 in-list)
(member ch2 in-list)))
(if (<= (position ch1 in-list :test #'char=)
(position ch2 in-list :test #'char=))
t
nil))))
(sort list-of-strings
#'(lambda(str1 str2) (cmp-func (char str1 8) (char str2 8))))))
Regards
Friedrich
················@bnfl.com (Mike Weston) writes:
> Hi,
>
> I am having a problem rearranging a list into a specified order and
> wondered if anyone could help? I want to sort the list by the 9th
> character (e.g. F in 'CEI2110.FC2) by a preset order I, R, C, F
>
> ('CEI2110.IL2 'CEI2110.IL1 'CEI2110.FC2 'CEI2110.FC1 'CEI1001.R
> 'CEi4070.C)
>
> => ('CEI2110.IL1 'CEI2110.IL2 'CEI1001.R 'CEI4070.C 'CEI2110.FC1
> 'CEI2110.FC2)
http://www.xanalys.com/software_tools/reference/HyperSpec/Body/fun_sortcm_stable-sort.html
--