Page 1 of 2

[Implemented] SortLinkedList()

Posted: Sun Mar 07, 2004 12:05 am
by Shopro
I am trying to code something that would sort a LinkedList structure, and I figured a command like this would be very handy:)

-Shopro

Posted: Sun Mar 07, 2004 1:31 am
by Shannara
I have to definately agree, such as sort by a certain structure's member?

Posted: Sun Mar 07, 2004 1:44 am
by Dare2
Would be a very nice feature!

Posted: Sun Mar 07, 2004 2:33 am
by Shannara
I knew I saw this somewhere, below are two different links, I havent tried them yet. However, one allows you to have a linklist in another linklist (usefull for custom GUI), and another is a sorted link list by a structure member :D :D :D Im looking to combine the two, as that is what I need.

Sorted Link List: http://www.reelmedia.org/pb/modules.php ... it&lid=127

Linklist in Linklist: http://www.reelmedia.org/pb/modules.php ... it&lid=195

Turns out the link-list in a link-list is worthless for us purists :) Those of us who are looking for cross-platform code. As it uses the WinAPI. however, the author did state that the WinAPI will be replaced when PB's memory library is usable :D

Posted: Sun Mar 07, 2004 5:20 am
by Shopro
This is what I did:

Code: Select all

;- Sort Linked List
;
Procedure Toppings_Sort()
  Protected Sort_MaxElements.l
  Protected Sort_NumberOfTimesSorted.l
  Protected Sort_CurrentElement.l
  Protected Sort_FirstElementToCompare.w, Sort_FirstElementToCompare45Pointer.l
  Protected Sort_SecondElementToCompare.w, Sort_SecondElementToComparePointer.l
  Protected Sort_SizeOfToppingStructure.l
  
  Flag_ToppingsEdited = #True
  
  Structure_Topping_Temp.Structure_Topping
  Sort_SizeOfToppingStructure = SizeOf(Structure_Topping)
  
  Sort_MaxElements = CountList(List_Structure_Topping())
  
  For Sort_NumberOfTimesSorted = 0 To Sort_MaxElements
    ResetList(List_Structure_Topping())
    For Sort_CurrentElement = 1 To Sort_MaxElements - Sort_NumberOfTimesSorted - 1
      NextElement(List_Structure_Topping())
      Sort_FirstElementToCompare = Asc(UCase(Left(List_Structure_Topping()\Name$, 1)))
      Sort_FirstElementToComparePointer = @List_Structure_Topping()
      
      NextElement(List_Structure_Topping())
      Sort_SecondElementToCompare = Asc(UCase(Left(List_Structure_Topping()\Name$, 1)))
      Sort_SecondElementToComparePointer = @List_Structure_Topping()
      
      If Sort_FirstElementToCompare > Sort_SecondElementToCompare
        CopyMemory(Sort_FirstElementToComparePointer, @Structure_Topping_Temp, Sort_SizeOfToppingStructure)
        CopyMemory(Sort_SecondElementToComparePointer, Sort_FirstElementToComparePointer, Sort_SizeOfToppingStructure)
        CopyMemory(@Structure_Topping_Temp, Sort_SecondElementToComparePointer, Sort_SizeOfToppingStructure)
      EndIf
      
      PreviousElement(List_Structure_Topping())
    Next
  Next
EndProcedure
it doesn't work as it is, so pls don't try to run it :o

Shannara: yes, to be able to sort by a certain structure's member

After coding the above, I looked at the Code Archives, and found a "ListBox Sort.pb". With this code...

Code: Select all

ListViewGadget(1, 10,10,280,280,#LBS_SORT | #LBS_NOINTEGRALHEIGHT | #LBS_HASSTRINGS)
and it sorts automatically! Where do these "#LBS_SORT"s come from? and is the same thing possible in a ListIcon???

Anyone?!

-Shopro

Posted: Sun Mar 07, 2004 2:56 pm
by horst

Posted: Sun Mar 07, 2004 5:43 pm
by Shopro
horst:
YES! This is exactly what I wanted!!

I will use your library in my project, thank you:)!

-Shopro

Posted: Sat Jul 03, 2004 1:53 pm
by eddy
@Horst

Rahh lovely :D :D
Your QLsort procedure rox !!

Posted: Wed Jul 04, 2007 10:23 pm
by dontmailme
I just found a need for this and I can report that it still rocks with 4.10b2 :D

Nice work!

Posted: Wed Jul 04, 2007 10:25 pm
by Trond
So you don't want to use SortStructuredList()?

Posted: Wed Jul 04, 2007 10:30 pm
by Kaeru Gaman
it seems to be a case of RTFM.... :lol:

Posted: Thu Jul 05, 2007 3:32 pm
by Heathen
Kaeru Gaman wrote:it seems to be a case of RTFM.... :lol:
:lol:

Posted: Thu Jul 05, 2007 3:38 pm
by netmaestro
For sure, it's definitely a case of Reorganize/Filter The Memory!

Posted: Thu Jul 05, 2007 7:06 pm
by Trond
netmaestro wrote:For sure, it's definitely a case of Reorganize/Filter The Memory!
RFTM <> RTFM!

Posted: Thu Jul 05, 2007 7:10 pm
by netmaestro
Sorry, my dylsexia is showing thru