Author Topic: A faster way to sort a list  (Read 2456 times)

0 Members and 1 Guest are viewing this topic.

Rabbit

  • Guest
A faster way to sort a list
« on: June 08, 2012, 12:53:55 PM »
I have a list that has 1500+ lines.

Each line has this as a list:
("1234" "Address" "City" "St" "Zip" "County" "First" "last")

So basically its a list of 1500+ lists.

I've written code to process this into the different type of data.  It's waaaaaaaay  to slow.  I let it run for about 5 minutes and it only got about 1/3 of the way through.  That's too long each time a drawing is started.

Does anybody know a faster method?

NomenclatureList = the list of lists

For ease, use this for testing.
(setq NomenclatureList (list (list "1234" "Address" "City" "St" "Zip" "County" "First" "last") (list "5678" "Address1" "City1" "St1" "Zip1" "County1" "First1" "last1") (list "9999" "Address2" "City2" "St2" "Zip2" "County2" "First2" "last2"))

Code: [Select]
(setq StoreNoList nil
        AddressList nil
        CityList nil
        ZipList nil
        CountyList nil
OOFirstNameList nil
OOLastNameList nil)
  (setq count (1- (length NomenclatureList)))
  (while (> count -1)
    (setq StoreNoList (acad_strlsort (cons (nth 0 (nth count NomenclatureList)) StoreNoList)))
    (setq AddressList (acad_strlsort (cons (nth 1 (nth count NomenclatureList)) AddressList)))
    (setq CityList (acad_strlsort (cons (nth 2 (nth count NomenclatureList)) CityList)))
    (setq StateList (acad_strlsort (cons (nth 3 (nth count NomenclatureList)) StateList)))
    (setq ZipList (acad_strlsort (cons (nth 4 (nth count NomenclatureList)) ZipList)))
    (setq CountyList (acad_strlsort (cons (nth 5 (nth count NomenclatureList)) CountyList)))
    (setq OOFirstNameList (acad_strlsort (cons (nth 6 (nth count NomenclatureList)) OOFirstNameList)))
    (setq OOLastNameList (acad_strlsort (cons (nth 7 (nth count NomenclatureList)) OOLastNameList)))
    (setq count (1- count))
  );while

Much appreciated,
Rabbit

hmspe

  • Bull Frog
  • Posts: 362
Re: A faster way to sort a list
« Reply #1 on: June 08, 2012, 01:34:10 PM »
What exactly are you trying to do?  It looks like your code produces a sorted list of store numbers, a separate sorted list of addresses, a separate sorted list of cities, etc.  Is that really what you want?  Sorting the full lines by store number would make some sense, but just having a sorted list of first names (with none of the other data included) doesn't seem too useful.

As to the code, you are sorting each sub-list every time a new entity is added to the sublist.  It would make more sense to build the sublists first [remove the (acad_strlsort from each line], then sort each sublist once. 
"Science is the belief in the ignorance of experts." - Richard Feynman

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: A faster way to sort a list
« Reply #2 on: June 08, 2012, 01:51:44 PM »
Code: [Select]
(mapcar (function (lambda (l v) (set v (acad_strlsort l))))
(apply 'mapcar (cons 'list NomenclatureList))
'(StoreNoList AddressList CityList StateList ZipList CountyList OOFirstNameList OOLastNameLis
)
)

pBe

  • Bull Frog
  • Posts: 402
Re: A faster way to sort a list
« Reply #3 on: June 08, 2012, 01:57:14 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun sortN  ( lst md / n)
  2.       (setq n 0)
  3.       (foreach
  4.              itm  '(StoreNoList
  5.                     AddressList
  6.                     CityList StateList
  7.                     ZipList
  8.                     CountyList
  9.                     OOFirstNameList
  10.                     OOLastNameList)
  11.             (set itm
  12.                  (vl-sort (mapcar '(lambda (i)
  13.                                 (nth n i))
  14.                  lst) md))
  15.             (setq n (1+ n))
  16.             ))

(sortn nomenclaturelist '<)
(sortn nomenclaturelist '>)

oops . forgot the sort part there.
« Last Edit: June 08, 2012, 02:09:58 PM by pBe »

kruuger

  • Swamp Rat
  • Posts: 637
Re: A faster way to sort a list
« Reply #4 on: June 08, 2012, 03:10:24 PM »
do you want to sort by specific column ?
Code - Auto/Visual Lisp: [Select]
  1. ; ======================================================================== ;
  2. ; Sortowanie listy / Sort list                                             ;
  3. ;  Lst  [LIST] - lista do sortowania / list to sort                        ;
  4. ;  Pos   [INT] - pozycja sortowania / sort position                        ;
  5. ;  Order [INT] - 0 = rosnaco / ascending                                   ;
  6. ;                1 = malejaco / descending                                 ;
  7. ; ------------------------------------------------------------------------ ;
  8. ; (cd:LST_SortList (list '(1 "b") '(4 "a") '(2 "c")) 0 1)                  ;
  9. ; ======================================================================== ;
  10. (defun cd:LST_SortList (Lst Pos Order)
  11.   (vl-Sort Lst
  12.     (function
  13.       (lambda (%1 %2)
  14.         ( (if (zerop Order) < >)
  15.           (nth Pos %1)
  16.           (nth Pos %2)
  17.         )
  18.       )
  19.     )
  20.   )
  21. )
Code - Auto/Visual Lisp: [Select]
  1. (cd:LST_SortList NomenclatureList 0 1)
sort by City, ascending
k.

Rabbit

  • Guest
Re: A faster way to sort a list
« Reply #5 on: June 08, 2012, 03:29:05 PM »
I am just now able to check in.  I haven't reviewed the codes provided here yet.

To answer the question(s), I'm setting up a dialog box where the user inputs in the store number.  I will then use the information in the list to get the rest of the information to populate the corresponding attributes in a titleblock.

I have a project that has to get out right this afternoon.  I hopefully will be able to look over the code supplied before the day is out.

Thanks for the input/knowledge guys.  It's very appreciated.

Rabbit

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: A faster way to sort a list
« Reply #6 on: June 08, 2012, 04:25:22 PM »
No need to sort the list, just use assoc like so
Code: [Select]
_$ (setq NomenclatureList (list (list "1234" "Address" "City" "St" "Zip" "County" "First" "last") (list "5678" "Address1" "City1" "St1" "Zip1" "County1" "First1" "last1") (list "9999" "Address2" "City2" "St2" "Zip2" "County2" "First2" "last2")))
(("1234" "Address" "City" "St" "Zip" "County" "First" "last") ("5678" "Address1" "City1" "St1" "Zip1" "County1" "First1" "last1") ("9999" "Address2" "City2" "St2" "Zip2" "County2" "First2" "last2"))
_$ (setq record (assoc "5678" NomenclatureList))
("5678" "Address1" "City1" "St1" "Zip1" "County1" "First1" "last1")
_$
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Rabbit

  • Guest
Re: A faster way to sort a list
« Reply #7 on: June 08, 2012, 04:41:13 PM »
You know.  There are some days where I try to make things way harder than they are supposed to be.  CAB just gave me the "slap my forehead" moment.  So much simpler and so much easier.  You guys are the best.  Thanks for the help.

Rabbit

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: A faster way to sort a list
« Reply #8 on: June 08, 2012, 05:11:16 PM »
TGIF  :-D
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

pBe

  • Bull Frog
  • Posts: 402
Re: A faster way to sort a list
« Reply #9 on: June 08, 2012, 11:18:19 PM »
No need to sort the list, just use assoc like so

I jumped the gun again. :-D
More often than not, members would only need to say what the final result/goal  will be and perhaps the gurus here will give you a straight answer on the fly.  :-D
Thread title would be like A faster wy to sort list [Goal/Result...]

But then again, its a good way to learn new stuff regardless the answers fits the question.

TGFC <-----  For CAB

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: A faster way to sort a list
« Reply #10 on: June 09, 2012, 12:17:08 AM »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.