msmith11

Negative Decimals Won't Sort Correctly using _ArraySort via _FileReadToArray

3 posts in this topic

Sorting negative decimals with _ArraySort has me baffled.  Trying to sort a 2D array with negative decimals read in thru _FileReadToArray.  Works fine for positive decimals.  For negative decimals preceded by a minus sign however, _ArraySort treats negative decimals as absolute values. The minus sign seems to be ignored.

Any suggestions would be greatly appreciated.  Thanks and best to you.

The  _FileReadToArray Version:

Copy this data to an empty Notepad file and save as Test.csv

CALD,0.0376
AXGN,0.0195
ERII,-0.0243
QRVO,-0.0025
BLDR,-0.0059
WTW,0.0620

;Program start
#include <File.au3>

Local $avArray[6][2] 

_FileReadToArray( "..path\Test.csv", $avArray, 0, "," )  ;YOU'll HAVE TO MODIFY YOUR PATH HERE

_ArrayDisplay($avArray)  ;starting data structure

 _ArraySort($avArray,0,0,0,1)  ;sort ascending on column 1 (the decimals)
_ArrayDisplay($avArray)

 _ArraySort($avArray,1,0,0,1)  ;sort descending on column 1 (the decimals)
_ArrayDisplay($avArray)

;Program End

;Negative Decimal Data acquired thru the UDF _FileReadToArray  will not sort correctly

**********************************************************************************************************

The same program with data entered manually:
#include <File.au3>

Local $avArray[6][2] = [ _
  ["CALD", 0.0376], _
  ["AXGN", 0.0195], _
  ["ERII", -0.0243], _
  ["QRVO", -0.0025], _
  ["BLDR", -0.0059], _
  ["WTW", 0.062]]

_ArrayDisplay($avArray)  ;starting data structure

 _ArraySort($avArray,0,0,0,1)
_ArrayDisplay($avArray)

 _ArraySort($avArray,1,0,0,1)
_ArrayDisplay($avArray)

;Manually entered negative decimals sort correctly

 

 

Share this post


Link to post
Share on other sites



I found something that works.  Once the decimals are isolated in their own cells you can datatype them to numbers using the Number function.
 

;New code:
#include <File.au3>

Local $avArray[6][2] 

_FileReadToArray( "..path\Test.csv", $avArray, 0, "," )  ;YOU'll HAVE TO MODIFY YOUR PATH HERE
_ArrayDisplay($avArray)  ;starting data structure

For $i = 0 to 5
       $a_AccumArray[$i][1] = Number( $a_AccumArray[$i][1] )
Next

_ArraySort($avArray,0,0,0,1)  ;sort ascending on column 1 (the decimals)
_ArrayDisplay($avArray)

_ArraySort($avArray,1,0,0,1)  ;sort descending on column 1 (the decimals)
_ArrayDisplay($avArray)

 

 

Share this post


Link to post
Share on other sites

msmith11,

you can datatype them to numbers

That is the only way to get numbers to sort "correctly" - otherwise they are sorted as strings.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now