WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Sort

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

ArraySort Intuitive Sort Explained

 Keywords: ArraySort Sort Order Negative Positive Word Integer Floating Point Vartype Intuitive Sort Explained 

Question:

When I try to sort the file on the first column and if the column contains negative values then the negative values appear above the highest positive values. With a descending sort I was assuming to see the highest positive value first and then the lowest negative value. Any ideas what I am doing wrong ?

Here is my data:

-00.14	07/04/12	91.64	1	91.64	92.20
+00.84	07/04/12	54.04	1	54.04	54.08
+02.21	07/04/12	49.53	1	49.53	49.83
+00.22	07/04/12	44.65	1	44.65	44.79
-01.92	07/04/12	44.29	1	44.29	44.35
+00.95	07/04/12	30.89	1	30.89	31.07
+01.10	07/04/12	23.84	1	23.84	24.03
+00.11	07/04/12	18.52	1	18.52	18.61
+02.35	07/04/12	06.54	1	06.54	06.55
Here is my script:
File_PortPreSort = "c:\temp\Portfolio_PreSort.txt"
File_PortSort = "c:\temp\Portfolio_Sort.txt"
filedelete(File_PortSort)
filarray_PreSort=ArrayFileGet(File_PortPreSort)
ArraySort( filarray_PreSort, @DESCENDING, 0, 0, ArrInfo(filarray_PreSort,1)-1 )
ArrayFilePut( File_PortSort, filarray_PreSort )
Run( File_PortSort, '' ) 

Answer:

  1. Because you do import the external data into a linear dim-1 array, so each entire line of the data file, having @TAB and other characters, will be stored as a string into one table cell, there is no more than one array column.
  2. Because you do sort strings, the descending collation sequence for text characters determines the sorting result, that means the minus "-" sign will appear on top of the list.
  3. Use function ArrayFileGetCSV to get column separated data.
  4. Having a dim-2 array with separated columns of data, in contradiction to the existing string datatype, the WinBatch array sort algorithm treats the first column as having numerical data.
That means the descending collation sequence for numbers puts positive numbers on top of the list. You are right, this is confusing, because in both cases array sorting is done on the same datatype string.
; Example for ambiguous sorting.

; Set file names.
DirChange (DirScript ())
strFileIn = "Portfolio_PreSort.txt"
strFileOut1 = "Portfolio_Sort.1.txt"
strFileOut2 = "Portfolio_Sort.2.txt"
strFileOut3 = "Portfolio_Sort.3.txt"

; For sure delete existing output files.
FileDelete (strFileOut1)
FileDelete (strFileOut2)
FileDelete (strFileOut3)

; Read data into dim-1 array and sort descending.
arrData = ArrayFileGet (strFileIn)
intVarType = VarType (arrData[0]) ; 2 = String.
ArraySort (arrData, @DESCENDING)
ArrayFilePut (strFileOut1, arrData)

; Read data into dim-2 array and sort descending on col-0.
arrData = ArrayFileGetCSV (strFileIn, 0, @TAB)
intVarType = VarType (arrData[0, 0]) ; 2 = string.
ArraySort (arrData, @DESCENDING, 0)
ArrayFilePutCSV (strFileOut2, arrData, @TAB, @TRUE, 2)

; Read data into dim-2 array, convert col-0 datatype to float and sort descending on col-0.
arrData = ArrayFileGetCSV (strFileIn, 0, @TAB)
intLast = ArrInfo (arrData, 1) - 1
For intI = 0 To intLast
   intVarType = VarType (arrData[intI, 0]) ; 2 = string.
   If IsFloat (arrData[intI, 0]) Then arrData[intI, 0] = 1.0 * arrData[intI, 0]
   intVarType = VarType (arrData[intI, 0]) ; 32 = floating point value.
Next
ArraySort (arrData, @DESCENDING, 0)
ArrayFilePutCSV (strFileOut3, arrData, @TAB, @TRUE, 2)

; Display results.
Run (strFileOut1, "")
Run (strFileOut2, "")
Run (strFileOut3, "")

:CANCEL
Exit

Article ID:   W17677
Filename:   ArraySort Intuitive Sort Explained.txt
File Created: 2012:07:05:09:14:32
Last Updated: 2012:07:05:09:14:32