Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Sorting arrays with built-in sorting functions

  1. #11
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,818
    Rep Power
    10
    Ciao John

    I've created a little script for testing and here it seems sorting just fine.
    I think there must be something else somewhere that corrupt the array data type or the array size or how data is loaded into array

    Here below my script that seems working fine here

    If you still have the problem, can you please create a short script showing the problem?

    Thanks a lot
    Eros


    #MinVersion 1.13
    
    
    uses "console"
    
    
    dim Numbers(100) as double =
    0.508210507 ,2.271638259 ,0.76078255 ,0.957525991,-2.387695394,0.795957904,0.693785993,-1.188321733,1.381440343,-0.461175478,
    1.489988646,0.750147094,0.527872561,-0.411497533,-0.634290479,0.178526739,1.285337463,-1.051731613,0.545955657,0.577297228,
    0.231946769,0.45728207,-0.735775852,-0.127960995,0.005307588,-0.050718714,0.524725502,0.295784074,-0.107940309,0.156720754,
    -0.941050506,-1.157671044,-0.323071971,0.148512737,-0.83228278,0.265810425,0.01064563,-0.028561661,1.846478155,0.217954152,
    1.364586147,-0.246613086,0.913975039,1.219781021,-1.100125379,-0.698041051,0.934720676,-0.982526268,0.502026823,-0.45267955,
    0.442353055,0.148805418,1.31759004,1.209563127,0.982567037,-0.760403384,0.070782254,1.374120987,-0.122692461,-0.174001986,
    0.807553832,0.160879446,0.979111885,0.407008646,0.750277918,-1.341988943,1.798166935,-0.014998984,-0.012918439,1.202851974,
    0.57190789,-0.730953737,1.19712005,1.086329786,-0.110994885,1.271766978,-1.695791348,1.127182143,-0.738788445,-0.362852264,
    1.029229745,-0.068066658,1.06387776,0.415527493,-0.225864695,-0.416020166,2.198364013,-0.809310952,-1.532119923,-0.542072656,
    -0.422387603,0.431662509,0.706261533,-0.823930064,0.011530519,-0.449438784,-0.286756,1.372709007,0.657282984,-0.147238732
    
    
    
    
    WaitKey(0, "Press a key to proceed: showing array data before and after sorting")
    
    
    printl "Numbers before sorting:"
      PrintArray(Numbers)
    
    
    WaitKey(0, "Press a key to sort array")
    
    
      array sort Numbers
      PrintArray(Numbers)
    
    
    WaitKey(0, "Press a key to sort end")
    
    
    function PrintArray(byref lArray() as double)
      long n
    
    
      for n = 1 to ubound(lArray)
        print format$(n, "000") in %CCOLOR_LIGHTBLUE, format$(lArray(n), "+#0.000000000;-#0.000000000; #0.000000000"), " "
        if mod(n, 5) = 0 then printl
      Next
    
    
    End Function
    
    Attached Files Attached Files
    Last edited by ErosOlmi; 26-08-2024 at 12:42.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  2. #12
    Junior Member
    Join Date
    Aug 2015
    Posts
    17
    Rep Power
    11
    Your script helped me pinpoint the issue.

    I was using Array Sort to sort all the elements (including empty cells) of an array that was DIMed to a value higher than the number of data points entered into it.

    As a result, Array Sort was correctly bringing in all the zero values of the empty cells right after the negative data points.

    In my program, I now use

    Array Sort xSort(1) for x4Sort
    
    where x4Sort is the actual number of values entered into the array.

    Problem solved, grazie tante Eros!

  3. #13
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,818
    Rep Power
    10
    Your post also helped me.
    Next thinBasic version 1.13.1 (out in few weeks) will have automatic dimensioning of string and numeric arrays when data is passed during variable declaration.

    I've also some other ideas I will work on.
    Will study how other languages syntax about this (php, javascript, python, ...) in order to simplify programmer's life.

    Array_Auto_Dim.jpg
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Page 2 of 2 FirstFirst 12

Similar Threads

  1. 3d arrays?
    By JosephE in forum thinDebug
    Replies: 2
    Last Post: 08-08-2010, 23:21
  2. Arrays
    By Charles Pegge in forum O2h Compiler
    Replies: 0
    Last Post: 17-03-2009, 16:09

Members who have read this thread: 6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •