asgarcymed Posted November 10, 2007 Share Posted November 10, 2007 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... Link to comment Share on other sites More sharing options...
Generator Posted November 10, 2007 Share Posted November 10, 2007 Look in StringSplit, FileReadLne, _ArraySort Link to comment Share on other sites More sharing options...
asgarcymed Posted November 11, 2007 Author Share Posted November 11, 2007 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 Can you please help me? Thanks in advance! MLMK - my blogging craziness... Link to comment Share on other sites More sharing options...
nfwu Posted November 11, 2007 Share Posted November 11, 2007 expandcollapse popup;;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) #) yyywww 1 TwitterOut of date stuff:Scripts: Sudoku Solver | Webserver | 3D library (Pure AutoIt) | Wood's GadgetsUDFs: _WoodUniqueID() | _DialogEditIni() | _Console*() | _GetIPConfigData() | _URLEncode/Decode() Link to comment Share on other sites More sharing options...
asgarcymed Posted November 11, 2007 Author Share Posted November 11, 2007 nfwu - Your code works perfectly!!! All the words of the world are not enough to say THANK YOU VERY MUCH!!!!!! MLMK - my blogging craziness... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now