0

I am trying to create an array or a list where I can put a numeric value together with a string and sort it by the numeric value. I understand how to sort the list. What I can't figure out is how to put the two values together in a container where I can work with them.

The code below produces results like this

  1. 0 / 11
  2. 112.312579750842 / 15
  3. 2.218114802558 / 16
  4. 416.368892799685 / 19
  5. 97.820969866657 / 24

Obviously the ArrayList is treating everything like a string so I am not able to sort these results numerically. What can I use in place of an ArrayList to get to where I need to be?

    Dim myLong As String = "-56.5896612"
    Dim myLat As String = "23.2266818"
    Dim Results As New Generic.Dictionary(Of String, Double)
    Dim row As DataRow

    For Each row In BO.DAC.ExecuteDataTable("GeoLocationSelect").Rows
        Dim miles As Double = (Radius.CalcDistance(myLat, _
                                                  myLong, _
                                                  CStr(row("Lat")), _
                                                  CStr(row("Lon"))))
        Results.Add(String.Format("{0} / {1}", miles, row("Store")))
    Next

    ' Updated. I added this below code, which i lifted from the link Brain2000
    ' posted. I do see my key pair in the debugger listing out correctly but
    ' I am unsure of how to get a hold of them now.

    Dim sortedDict = _
    (From entry In Results Order By entry.Value Ascending).ToDictionary(Function(pair) pair.Key, _
                                                                        Function(pair) pair.Value)

    For Each res In sortedDict
    ' How do I get to the values here (for instance to write that key value pair out to the debug windows).
    Next
Buck Hicks
  • 1,534
  • 16
  • 27
  • 2
    I would make a new class with that stores the lat and long, move the CalcDistance call to it, and implement IComparable, then push it into a List type. – asawyer Jul 22 '11 at 18:13

2 Answers2

0

Use a Dictionary(Of Interger, String) instead of an arraylist You can sort the Keys of the dictionary and then get the corresponding values

Eddy
  • 5,320
  • 24
  • 40
  • No, you can't sort a dictionary. You can steal @asawyer's comment. – Hans Passant Jul 22 '11 at 18:16
  • @Hans I'd have made it an answer but no compiler in front of me today, so example code would be hard to provide. – asawyer Jul 22 '11 at 18:20
  • @Hans read my answer again: you can sort THE KEYS. Then loop through the sorted keys and for each of them get the corresponding value. – Eddy Jul 22 '11 at 18:32
  • Example code (string keys, int values but thats not relevant) here: http://www.dotnetperls.com/sort-dictionary-vbnet – Eddy Jul 22 '11 at 18:39
0

A SortedDictionary or Linq should do the trick. This was discussed in more detail in the following question:

How do you sort a dictionary by value?

Community
  • 1
  • 1
Brain2000
  • 4,655
  • 2
  • 27
  • 35
  • Based on what I see in the debugger I think I have this so it will work but I am not sure how to pull the items out of the sorted dictionary now. I updated my code above to show what I have now. – Buck Hicks Jul 22 '11 at 21:41
  • Try this: For Each res In (From entry In Results Order By entry.Value Ascending Select entry.Key).ToArray() – Brain2000 Jul 22 '11 at 22:07
  • Even better: For Each res In (From entry In Results Order By entry.Value Ascending Select entry.Key) – Brain2000 Jul 22 '11 at 22:13
  • Thank you that has been very helpful. I can now see all the items listed in the debugger (stepping through them one at a time). How do I hook into each one? For instance when I want to print the store and miles to the screen with console.writeline. Just using Console.Writeline(res) gives me the stores but not the miles. – Buck Hicks Jul 25 '11 at 14:00
  • Never mind it got it now. I just needed to add Select entry.Key, entry.Value to the for each loop and then use res.key or res.value. – Buck Hicks Jul 25 '11 at 14:09