Class Array Sort Question

I know from the docs Sort and SortWith will only work with Double, single, String, and Integers.

I am implementing a priority queue array of custom class objects and would really like to sort it based on one of the class object element value integers. Does anyone have a recommendation for this? My priority queue array will consist of about 180 class object elements.

I am doing a loop and picking out the lowest element value, however it would be nice to sort this and then just pop the 0 element everytime which would enhance my speed.

Thank you in advance!
Mike

[code]Dim elements() As PriorityQueueElement
Dim elementValues() As Integer

For i As Integer = 0 To elements.Ubound
elementValues.Append(elements(i).Value)
Next

elementValues.SortWith(elements)[/code]

[quote=121677:@Eli Ott][code]Dim elements() As PriorityQueueElement
Dim elementValues() As Integer

For i As Integer = 0 To elements.Ubound
elementValues.Append(elements(i).Value)
Next

elementValues.SortWith(elements)[/code][/quote]
Thanks Eli and Ill see how I can l keep the two arrays in sync. Thank you and I appreciate your help.

note: when I remove the sorted element integer array element[0] then I have to somehow make sure that the same element reference somewhere in PriorityQueueClassArray() is also removed.

There is surely only one array with your priority queue elements, so I don’t know what you mean by that:[quote=121680:@Mike Cotrone]note: when I remove the sorted element array element[0] then I have to somehow make sure that the same element reference somewhere in PriorityQueueClassArray() is also removed.[/quote]

The problem with moving the selected property values from the priority queue class array into a separate Integer Array provides me an issue since I still need to constantly use the class properties or the original priority queue for cost decisions.

Hopefully I am making sense as writing about this isn’t as easy as I would have thought :slight_smile:

My priorityArray is an array of classes with about 6 properties. I need to make decisions based on some of these properties ie. the cost property. The best would be the ability to sort this same array based on each entries class cost property, but I know I can’t do that. If I do like you had suggested that would give me a nice quick sort and then pull the top element off that array. The problem is that now I need to go back to the Priorityqueue array, find that referenced element I removed from the sorted array, then search again fro the next lowest element cost property value. It won’t work without major array element reference management I would think … Right?

Grab a reference to element 0 before you remove it:

Dim element As PriorityQueueElement = PriorityQueueElementArray(0) PriorityQueueElementArray.Remove(0)
There is really no need for two arrays - as far as I understand your situation of course.

[quote=121698:@Eli Ott]Grab a reference to element 0 before you remove it:

Dim element As PriorityQueueElement = PriorityQueueElementArray(0) PriorityQueueElementArray.Remove(0)
There is really no need for two arrays - as far as I understand your situation of course.[/quote]
Thank you Eli… I mis read your earlier example and I will try this .Thank you again.

Mike, the elementValues array is a throw-away. Reconstruct it each time you need to sort and then discard it.

Thanks Tim! That is what I have been working on and much appreciated with your confirmation.

Have a great weekend!

Eli and Tim both thank you. I really didn’t quite understand what sortwith was meant for until I implemented a version of Eli’s example. Thank you again guys the sorting of my arrays are working 100%. :wink:

  Dim FCost_Array() as integer
  
  for y = 0 to UBound(PriorityQ_Open)
    
    FCost_Array.Append PriorityQ_Open(y).F_cost
    
  Next y
  
  FCost_Array.SortWith(PriorityQ_Open)

The FCost_Array is my throw away array. I was under the wrong impression that SortWith was like Sort in which you had to manually handle the two array linking. Sortwith magically sorts both arrays based on the integer value in my throw away array as I needed. Then I just continue on with my newly sorted PriorityQ_Open Array of classes. perfect.

You can get a little bit more performance from it if you avoid the appends and simply redim your temp array.

On my phone or would give you an example. I’m sure someone else can chime in on this point.

[quote=121789:@Jeremy Cowgar]You can get a little bit more performance from it if you avoid the appends and simply redim your temp array.

On my phone or would give you an example. I’m sure someone else can chime in on this point.[/quote]
Jeremy you mean the .append inside of the for loop?

Yes. I’ll try on my phone.

Dim f() As Integer
Redim f(q.Ubound)

For idx As Integer = 0 To q.Ubound
  f(idx) = q(idx).F_Cost
Next

Shortened variable names but I think you can get the idea.

Charles Yeoman’s SortLibrary is another option. It seems to still be available here

The external mirror link goes to declaresub.com where the page is gone. The Softpedia link gets you version 2.3 which I have been using for years.

I’m sure I’m not alone in missing Charles’ voice in these forums/mailing list.