WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: hdsouza on December 29, 2014, 02:05:16 PM

Title: Sort array based on second column
Post by: hdsouza on December 29, 2014, 02:05:16 PM
Is it possible to sort an array based on the 2nd (or n th column)
A roundabout way would be rewriting the file, making the 2nd column into the 1st column, then sorting it and then writing it back the way the columns need to be placed, although I was hoping there was an easier way


Sorting it based on 1st column would be
Code (winbatch) Select

filarray_Unsorted =ArrayFileget(File_Unsorted)
ArraySort(filarray_Unsorted, @ASCENDING, 0, 0, ArrInfo(filarray_Unsorted,1)-1)
ArrayFileput(File_Sorted, filarray_Unsorted)
Title: Re: Sort array based on second column
Post by: td on December 29, 2014, 02:19:34 PM
Simple enough. To sort on the second column

Code (winbatch) Select
ArraySort(filarray_Unsorted, @ASCENDING, 1)
Title: Re: Sort array based on second column
Post by: hdsouza on December 29, 2014, 09:47:14 PM
Quote from: td on December 29, 2014, 02:19:34 PM
Simple enough. To sort on the second column

Code (winbatch) Select
ArraySort(filarray_Unsorted, @ASCENDING, 1)

I get Error 1830 - Sort column is out of range on ArraySort(filarray_Unsorted, @ASCENDING, 1)
Title: Re: Sort array based on second column
Post by: td on December 30, 2014, 08:21:38 AM
Your array does not have 2 dimensions... I should have added that if your file has delimited columns then try using ArrayFilePutCsv to create a 2 dimensional array.  If your file has fixed length records and fixed length fields with a single key type you can just use BinarySort.  Full documentation for ArraySort, ArrayFilePutCsv and BinarySort can be found in the help file.