Comment trier une liste de listes selon un ordre défini dans une autre liste ?

Bonjour à la communauté.
Je cherche à trier une liste de listes nommée LstPiece
(construite avec un : (ssget « _X » '((0 . « INSERT ») (2 . « TC_NomPiece »))))

(("Cuisine" 11.8 0.0 "00" "Hum" "Hum")
 ("S. manger" 16.2 0.0 "00" "Hum" "Jour")
 ("Salon" 16.1 0.0 "00" "Hum" "Jour")
 ("Entrée" 5.9 0.0 "00" "Jour" "Jour")
 ("Chambre 1" 12.1 0.0 "00" "Jour" "Nuit")
 ("Dress. 1" 4.6 0.0 "00" "Jour" "Nuit")
 ("S. eau 1" 4.1 0.0 "00" "Jour" "Hum")
 ("Dgt" 4.1 0.0 "01" "Jour" "Hab")
 ("Salon" 16.2 2.7 "01" "Jour" "Jour")
 ("Chambre 2" 14.4 3.0 "01" "Jour" "Nuit")
 ("Chambre 3" 14.0 2.9 "01" "Jour" "Nuit")
 ("S. bains" 8.0 1.3 "01" "Jour" "Hum")
 ("W.C." 1.3 0.0 "01" "Jour" "Hum")
)

Selon l’ordre d’une autre liste nommée LstPieceTri (c’est là que je bloque) :

("Entrée" "S. manger" "Salon" "Espace de vie" "Cuisine" "Chambre" 
"S. bains" "S. eau" "W.C." "Toil." "Cellier" "Couloir" "Dégagement" 
"Débarras" "Dress." "Pl." "Escalier" "Balcon" "Loggia" "Terrasse" 
"Cave" "Stationnement")

Et sachant aussi que certaines pièces pourront porter une « incrémentation » :
Chambre 1, Chambre 2, Dgt 1, Dgt 2

Je ne sais pas si j’ai été très clair, j’ai fait au mieux :wink:.
Si quelqu’un a une astuce, un conseil, une idée… Je suis preneur.
Bien à toi la communauté.
Denis…

Bonjour Patrick.
Je lis dans mon sujet « a attribué le message à autolisp_experts » avec un lien.
Mais je n’ai pas d’accès, est-ce un groupe qui regardera ce message ?
Bonne journée.
Denis…

C’est ça oui c’est pour attirer l’attention de ce groupe sur cette question.
Tu dois pouvoir voir les membres du groupe :
https://dessein-tech.com/g/autolisp_experts

Tu as essayé Perplexity AI pour AutoLISP ?

Parfait, merci beaucoup.
J’utilise souvent Perplexity, mais pour des codes « simples », mais dans ce cas, je ne sais pas trop comment procéder.
J’ai tout de même posé la question à Perplexity, qui me répond exactement ce que je cherche (heureuse surprise).
Mais ça ne fonctionne pas à cause des incrémentations de certaines pièces (Chambre 1, Chambre 2…).
Mais je pense avoir trouvé la solution…
Je pense avec quelque chose comme : (substr "Chambre 1" 1 (strlen "Chambre")).

Voici la réponse de Perplexity :

(setq ordre '("B" "C" "A"))
(setq listes '(("A" 1 2) ("C" 3 4) ("B" 5 6)))
(defun comparer (a b)
  (< (vl-position (car a) ordre)
     (vl-position (car b) ordre)))
(setq triées (vl-sort listes 'comparer))

C’est l’exacte réponse à la question, mais il y a l’incrémentation de certaines pièces, et je n’arrive pas à implanter le substr dans ce code…

Je pensais faire une fonction avec un tri à bulle, mais là, je bute…
Peut-être avec insertAt et removeAt de @gilecad ?

Coucou @DenisH,
Je me permet de répondre rapidement sur la considération de la racine de ta chaîne de caractère (pour une meilleure comparaison). As-tu pensé à la fonction (vl-string-trim) ou simplement (vl-string-right-trim) ? Dans ton cas se serait :

(vl-string-right-trim "0123456789 " nomPiece)

En clair, cela permet de supprimer chaque caractères présent dans la première chaîne donnée (donc 0, 1, 2, …, 9) de la seconde chaîne. En ajoutant l’espace dans les caractères à supprimer, cela évite aussi des erreurs à ce niveau-là. Même si ce n’est pas parfait comme solution, cela peut te servir de piste pour raccorder les morceaux :slight_smile:
Dans ma bibliothèque de fonctions j’ai le programme suivant qui pourrait te servir par exemple:


; +-----------------------------------------------------------------------------------------------------------------------------------------------+ ;
; |                                                                                                                                               | ;
; |                                                   HISTORICAL TRACKING FILE OF THE FUNCTION                                                    | ;
; |                                                           --{  get-pattern-list  }--                                                          | ;
; |                                                                                                                                               | ;
; +-----------------------------------------------------------------------------------------------------------------------------------------------+ ;



;                                      []-----------------------[] get-pattern-list []-----------------------[]                                     ;
;--- Date of creation       > 20/03/2020                                                                                                            ;
;--- Last modification date > 25/06/2020                                                                                                            ;
;--- Author                 > Luna                                                                                                                  ;
;--- Version                > 3.0.0                                                                                                                 ;
;--- Class                  > "BaLst"                                                                                                               ;

;--- Goal and utility of the main function                                                                                                          ;
;   For each element of a given list, allows to separate a numerical index at the end and/or the beginning of the string which is identified as     ;
;   pattern. Each element of the initial list is therefore decomposed in the form of a list with the initial value of the element (type             ;
;   conservation) as a header, then the decomposition list corresponding to the numerical index at the beginning of the string, then to the pattern ;
;   (character string from which the numerical values at the end of the string have been removed) followed by its numerical index at the end of the ;
;   string (kept in the 'STR format like the first one).                                                                                            ;
;                                                                                                                                                   ;
;--- Declaration of the arguments                                                                                                                   ;
; The function (get-pattern-list) have 2 argument(s) :                                                                                              ;
;   --•  lst                    > corresponds to the list of simple elements that we want to decompose                                              ;
;     (type lst) = 'LST                         | Ex. : '("Layer10" "Bloc" 8 "Layer1" "Layer3" "Bloc5" 4 88 "90" "Bloc30" "Layer7"), ...            ;
;   --•  flag                   > determines if you want to ignore the case, which can alter the way of sorting afterwards. The alphabetical        ;
;                               sorting is done according to the ascii value of the characters, so the lower case letters (from 97 to 122 in ascii  ;
;                               value) located after the upper case letters (from 65 to 90 in ascii value)                                          ;
;     (type flag) = 'INT                        | Ex. : 0 to get uppercase patterns, 1 to get lowercase patterns or 2 to keep the format of pattern ;
;                                                                                                                                                   ;
;--- List of dependent's functions                                                                                                                  ;
;   --•  "XxXxx" ---> ...                                           | v#.#.# - ##/##/#### (Luna)                                                    ;
;                                                                                                                                                   ;
;--- List of local functions                                                                                                                        ;
;   --•  "XxXxx" ---> ...                                           | v#.#.# - ##/##/#### (Luna)                                                    ;
;                                                                                                                                                   ;
;--- List of programs using this function                                                                                                           ;
;   --•  "BaLst" ---> sort-list                                     | v1.3.1 - 06/05/2022 (Luna)                                                    ;
;                                                                                                                                                   ;
;--- Return                                                                                                                                         ;
;   The function (get-pattern-list) returns a list composed of pointed pairs whose header corresponds to the initial element, and whose value       ;
;   corresponds to a list composed of the numerical index at the beginning of the string (if the element does not have an index at the beginning of ;
;   the string, the pattern is equivalent to "") the identified pattern whose case depends on the value of the flag argument (if the element does   ;
;   not have a pattern, i.e. it is only a sequence of numbers, the pattern is equivalent to "") and its numerical index located at the end of the   ;
;   string (if the element does not have an index at the end of the string, it is equivalent to ""). This list is then processed by the function    ;
;   (sort-list).                                                                                                                                    ;
;     Ex. : (get-pattern-list '("000-UBS-Modules 2" "0" "Pvcase Offset" "TEXT_101" "Text_2") 0) returns                                             ;
;               ( ("000-UBS-Modules 2" ("000" "-UBS-MODULES " "2"))                                                                                 ;
;                 ("0" ("0" "" ""))                                                                                                                 ;
;                 ("Pvcase Offset" ("" "PVCASE OFFSET" ""))                                                                                         ;
;                 ("TEXT_101" ("" "TEXT_" "101"))                                                                                                   ;
;                 ("Text_2" ("" "TEXT_" "2"))                                                                                                       ;
;               )                                                                                                                                   ;
;           (get-pattern-list '("000-UBS-Modules 2" "0" "Pvcase Offset" "TEXT_101" "Text_2") 1) returns                                             ;
;               ( ("000-UBS-Modules 2" ("000" "-ubs-modules " "2"))                                                                                 ;
;                 ("0" ("0" "" ""))                                                                                                                 ;
;                 ("Pvcase Offset" ("" "pvcase offset" ""))                                                                                         ;
;                 ("TEXT_101" ("" "text_" "101"))                                                                                                   ;
;                 ("Text_2" ("" "text_" "2"))                                                                                                       ;
;               )                                                                                                                                   ;
;           (get-pattern-list '("000-UBS-Modules 2" "0" "Pvcase Offset" "TEXT_101" "Text_2") 2) returns                                             ;
;               ( ("000-UBS-Modules 2" ("000" "-UBS-Modules " "2"))                                                                                 ;
;                 ("0" ("0" "" ""))                                                                                                                 ;
;                 ("Pvcase Offset" ("" "Pvcase Offset" ""))                                                                                         ;
;                 ("TEXT_101" ("" "TEXT_" "101"))                                                                                                   ;
;                 ("Text_2" ("" "Text_" "2"))                                                                                                       ;
;               )                                                                                                                                   ;
;                                                                                                                                                   ;
;--- Historic list of the version with their modification status                                                                                    ;
; +------------+----------------------------------------------------------------------------------------------------------------------------------+ ;
; |   v3.0.0   |   Added recognition of the numerical index at the beginning of the string and added the flag argument to take into account the   | ;
; |            |   case of the patterns obtained                                                                                                  | ;
; +------------+----------------------------------------------------------------------------------------------------------------------------------+ ;
; |   v2.0.0   |   Removal of the function (vl-remove), correction of the method applied to avoid the bugs encountered for strings composed only  | ;
; |            |   of numerical values                                                                                                            | ;
; +------------+----------------------------------------------------------------------------------------------------------------------------------+ ;
; |   v1.0.0   |   Creation of the function                                                                                                       | ;
; +------------+----------------------------------------------------------------------------------------------------------------------------------+ ;
;                                                                                                                                                   ;

(defun get-pattern-list (lst flag)
  (mapcar
    '(lambda (x / tag str value)
      (if (= "." (setq str (vl-string-trim "0123456789" (setq tag (vl-princ-to-string x)))))
        (setq str "")
      )
      (setq
        value
          (list
            (if (wcmatch tag "#*")
              (if (= str "")
                tag
                (substr tag 1 (vl-string-search (substr str 1 1) tag))
              )
              ""
            )
            (cond
              ((= flag 0) (strcase str))
              ((= flag 1) (strcase str t))
              (t str)
            )
            (if
              (and
                (/= str "")
                (wcmatch tag "*#")
              )
              (substr tag (1+ (strlen (vl-string-right-trim "0123456789" tag))))
              ""
            )
          )
      )
      (cons x (list value))
     )
    lst
  )
)

Avec un exemple tout simple d’utilisation ici pour ma fonction (sort-list) :


; +-----------------------------------------------------------------------------------------------------------------------------------------------+ ;
; |                                                                                                                                               | ;
; |                                                   HISTORICAL TRACKING FILE OF THE FUNCTION                                                    | ;
; |                                                              --{  sort-list  }--                                                              | ;
; |                                                                                                                                               | ;
; +-----------------------------------------------------------------------------------------------------------------------------------------------+ ;



;                                         []-----------------------[] sort-list []-----------------------[]                                         ;
;--- Date of creation       > 20/03/2020                                                                                                            ;
;--- Last modification date > 06/05/2022                                                                                                            ;
;--- Author                 > Luna                                                                                                                  ;
;--- Version                > 1.3.1                                                                                                                 ;
;--- Class                  > "BaLst"                                                                                                               ;

;--- Goal and utility of the main function                                                                                                          ;
;   Allows you to sort a list by ignoring the case of strings and sorting the elements by their pattern and then by their numerical indexing.       ;
;                                                                                                                                                   ;
;--- Declaration of the arguments                                                                                                                   ;
; The function (sort-list) have 2 argument(s) :                                                                                                     ;
;   --•  lst                    > corresponds to the list we want to sort                                                                           ;
;     (type lst) = 'LST                         | Ex. : '("Layer10" "Bloc" 8 "Layer1" "Layer3" "Bloc5" 4 88 "90" "Bloc30" "Layer7"), ...            ;
;   --•  fun                    > determines the function we want to apply to sort the list                                                         ;
;     (type fun) = 'SYM                         | Ex. : '< (= ascending order), '> (= descending order), ...                                        ;
;                                                                                                                                                   ;
;--- List of dependent's functions                                                                                                                  ;
;   --•  "BaLst" ---> get-pattern-list                              | v3.0.0 - 25/06/2020 (Luna)                                                    ;
;                                                                                                                                                   ;
;--- List of local functions                                                                                                                        ;
;   --•  "XxXxx" ---> ...                                           | v#.#.# - ##/##/#### (Luna)                                                    ;
;                                                                                                                                                   ;
;--- List of programs using this function                                                                                                           ;
;   --•  "BaLst" ---> DXF_List                                      | v1.2.2 - 06/05/2022 (Luna)                                                    ;
;                                                                                                                                                   ;
;--- Return                                                                                                                                         ;
;   The function (sort-list) returns the list sorted according to two levels: the first level corresponds to the pattern of each element of the     ;
;   list (obtained thanks to (get-pattern-list)) ignoring the case and the second level corresponds to the numerical index for each pattern. The    ;
;   type of the elements does not affect the sorting of the elements.                                                                               ;
;     Ex. : (sort-list '("Layer10" "Bloc" 8 "Layer1" "Layer3" "Bloc5" 4 88 "90" "Bloc30" "Layer7") '<) returns                                      ;
;             (4 8 88 "90" "Bloc" "Bloc5" "Bloc30" "Layer1" "Layer3" "Layer7" "Layer10")                                                            ;
;           (sort-list '("Layer10" "Bloc" 8 "Layer1" "Layer3" "Bloc5" 4 88 "90" "Bloc30" "Layer7") '>) returns                                      ;
;             ("Layer10" "Layer7" "Layer3" "Layer1" "Bloc30" "Bloc5" "Bloc" "90" 88 8 4)                                                            ;
;                                                                                                                                                   ;
;--- Historic list of the version with their modification status                                                                                    ;
; +------------+----------------------------------------------------------------------------------------------------------------------------------+ ;
; |   v1.3.1   |   Correction since the release of (get-pattern-list) 3.0.0 and renames 'flag' into 'fun"                                         | ;
; +------------+----------------------------------------------------------------------------------------------------------------------------------+ ;
; |   v1.3.0   |   Modification of the function (get-pattern-list) to the version 3.0.0 with one more argument                                    | ;
; +------------+----------------------------------------------------------------------------------------------------------------------------------+ ;
; |   v1.2.0   |   Modified the use of the flag argument for more readability and flexibility on the sorting method to apply                      | ;
; +------------+----------------------------------------------------------------------------------------------------------------------------------+ ;
; |   v1.1.0   |   Modification of the function (get-pattern-list) associated with it, correction of the bugs induced in the case of a string     | ;
; |            |   containing only numbers and optimization of the calculation in a single loop                                                   | ;
; +------------+----------------------------------------------------------------------------------------------------------------------------------+ ;
; |   v1.0.0   |   Creation of the function                                                                                                       | ;
; +------------+----------------------------------------------------------------------------------------------------------------------------------+ ;
;                                                                                                                                                   ;

(defun sort-list (lst fun)
  (mapcar 'car
    (vl-sort
      (get-pattern-list lst 2)
      '(lambda (a b)
        (setq a (cadr a) b (cadr b))
        (if (= (car a) (car b))
          (if (= (cadr a) (cadr b))
            ((eval fun) (atoi (caddr a)) (atoi (caddr b)))
            ((eval fun) (cadr a) (cadr b))
          )
          ((eval fun) (atoi (car a)) (atoi (car b)))
        )
       )
    )
  )
)

(PS: les extensions « .lsp » ne sont pas supportées sur ce site ?)

Bisous,
Luna

1 « J'aime »

Si, il faut indiquer lisp après les trois « quotes » car parfois la détection auto du code ne fonctionne pas, je suppose à cause des commentaires, voir :

J’ai modifié ton message…