Jump to content
Sign in to follow this  
asgarcymed

How to sort numerically a CSV file?

Recommended Posts

asgarcymed

I have a CSV (Comma-Separated Values) file:

Name,Score

Terry,1

Chris,10

Andy,2

David,20

And I want to sort it numerically (like this):

Name,Score

Terry,1

Andy,2

Chris,10

David,20

A CSV file is a text file; the delimiter is a comma; each line has one record.

Please help!

Thanks in advance.

Best regards.


MLMK - my blogging craziness...

Share this post


Link to post
Share on other sites
Generator

Look in StringSplit, FileReadLne, _ArraySort

Share this post


Link to post
Share on other sites
asgarcymed

I tried:

#include <array.au3>
_CSVSortLineArray(@DesktopDir & "\names.1.csv", @DesktopDir & "\names.2.csv", 0, 2, Chr(13))
Func _CSVSortLineArray($hFileIn, $hFileOut, $nDescending = 0, $nStartSort = 1, $vDelimSort = ",", $vDelimOut = @CRLF)
    Local $sReadFile = FileRead($hFileIn)
    If StringRight($sReadFile, StringLen($vDelimSort)) = $vDelimSort Then _
            $sReadFile = StringTrimRight($sReadFile, StringLen($vDelimSort))
    Local $avArray = StringSplit($sReadFile, $vDelimSort, 1)
    _ArraySort($avArray, $nDescending, $nStartSort)
    Local $sWrite
    For $iCC = 1 To $avArray[0]
        $sWrite &= $avArray[$iCC] & $vDelimOut
    Next
    FileClose(FileOpen($hFileOut, 2))
    Return FileWrite($hFileOut, StringTrimRight($sWrite, StringLen($vDelimOut)))
EndFunc

But it did not work :P

Can you please help me?

Thanks in advance!


MLMK - my blogging craziness...

Share this post


Link to post
Share on other sites
nfwu

;;Step 1: Read data from file into an array

$filenamein = "in.csv"
$filehandle = FileOpen($filenamein, 0)
Dim $data[1024][2]
$count = 0
While 1
   $read = FileReadLine($filehandle)
   If @Error = -1 Then ExitLoop
   $read = StringSplit($read, ",")
   $data[$count][0] = $read[1]
   $data[$count][1] = $read[2]
   $count += 1
WEnd
FileClose($filehandle)

;; Step 2: Resize the array to the correct size

ReDim $data[$count][2]

;; Step 3: Preform a sort operation, in this case it is the "Bubble Sort" algorithum.

$swapped = 1
While $swapped = 1
   $swapped = 0
   For $i = 0 to $count-2
      If Number($data[$i][1]) > Number($data[$i+1][1]) Then
         ;;Swap values
         $temp1 = $data[$i][1]
         $temp2 = $data[$i][0]
         $data[$i][1] = $data[$i+1][1]
         $data[$i][0] = $data[$i+1][0]
         $data[$i+1][1] = $temp1
         $data[$i+1][0] = $temp2
         $swapped = 1
      EndIf
   Next
WEnd

;; Step 4: Output result to file

$fileout = "out.csv"
$filehandle = FileOpen ( $fileout, 2 )
For $i = 0 to $count-1
   FileWriteLine($filehandle, $data[$i][0]&","&$data[$i][1])
Next
FileClose($filehandle)

#)

Share this post


Link to post
Share on other sites
asgarcymed

nfwu - Your code works perfectly!!! All the words of the world are not enough to say THANK YOU VERY MUCH!!!!!!

;):P


MLMK - my blogging craziness...

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.