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.
[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
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.
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%.
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.
[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?