locked
Sort for array? RRS feed

  • Question

  • Hi,

    any idea for some kind of sort (maybe bubble sort for numeric values), just found an example for string sort in the forum.

    Thanks.
    Wednesday, January 6, 2010 7:58 PM

Answers

  • Sorting an array of numbers should be easier than a string list, try the following to get started - a bit more work work be needed using Array.GetAllIndices if the array is not always a list indexed by integers from 1 to the number of elements in the array.

    'Create array of random numbers to sort
    nvar = 20
    For i = 1 To nvar
      var[i] = Math.GetRandomNumber(100)
    EndFor

    'Sort the variables
    SortVar()

    'Output the sorted list
    For i = 1 To nvar
      TextWindow.WriteLine(var[i])
    EndFor

    Sub SortVar
      'bubble sort variables - must be in array called var
      For i = 1 To Array.GetItemCount(var)-1
        For j = i+1 To Array.GetItemCount(var)
          If (var[j] < var[i]) Then
            temp = var[i]
            var[i] = var[j]
            var[j] = temp
          EndIf
        EndFor
      EndFor
    EndSub


    • Marked as answer by Vijaye Raji Friday, January 8, 2010 4:41 PM
    Wednesday, January 6, 2010 8:32 PM