Jump to content

Sorting By "Custom Rules"


 Share

Recommended Posts

16 hours ago, kylomas said:

Batman22,

This worked for me...I don't have any idea what is wrong on your end.  Also, you should interrogate @error after call _FileWriteFromArray().

#include <array.au3>
#include <file.au3>
#include <sqlite.au3>

local $array = sortme(@ScriptDir & '\samplefile.txt')
_FileWriteFromArray(@ScriptDir & '\samplefile.txt', $array, Default, Default, " ")

shellexecute(@ScriptDir & '\samplefile.txt')

Func SortMe($filename)

    _SQLite_Startup()
    _SQLite_Open()
    OnAutoItExitRegister('_fini')


    Local $ret = _SQLite_Exec(-1, 'CREATE TABLE if not exists [t1] (c1, c2);')
    If $ret <> $SQLITE_OK Then Exit (MsgBox(0, 'SQLITE ERROR', 'Create Temp Table Failed'))

    ; load temp table t1

    Local $aFile = StringSplit(FileRead($filename), @CRLF, 3), $sql = 'insert into t1 values '

    For $1 = 0 To UBound($aFile) - 1
        If $aFile[$1] = '' Then ContinueLoop

        ; prefix sort order control byte
        Switch True
            Case StringRegExp($aFile[$1], '^M.*')
                $aFile[$1] = '0' & $aFile[$1]
            Case StringRegExp($aFile[$1], '^LCSD.*')
                $aFile[$1] = '1' & $aFile[$1]
            Case StringRegExp($aFile[$1], '^LCS-.*')
                $aFile[$1] = '2' & $aFile[$1]
            Case Else
                $aFile[$1] = '3' & $aFile[$1]
        EndSwitch

        $sql &= '(' & _SQLite_FastEscape(StringRegExpReplace($aFile[$1], '(.*?) .*', '$1')) & ', ' & _SQLite_FastEscape(StringRegExpReplace($aFile[$1], '.*? (.*)', '$1')) & '),' & @CRLF
    Next

    $sql = StringTrimRight($sql, 3) & ';'

    _SQLite_Exec(-1, $sql)
    If @error Then Exit MsgBox(0, '', _SQLite_ErrMsg())

    ; get rid of dup entries...stor result in table t2

    Local $ret = _SQLite_Exec(-1, 'CREATE TABLE t2 AS SELECT distinct * FROM t1; drop table t1')
    If $ret <> $SQLITE_OK Then Exit (MsgBox(0, 'SQLITE ERROR', 'Create Final Table Failed'))

    ; retrieve entries ordered by column 1 desc within column 2 asc

    Local $ret, $arows, $irows, $icols
    _SQLite_GetTable2d(-1, 'select * from t2 order by c2, c1;', $arows, $irows, $icols)

    ;strip sort order control byte
    For $1 = 1 To UBound($arows) - 1
        $arows[$1][0] = StringTrimLeft($arows[$1][0], 1)
    Next


    Return $arows

EndFunc   ;==>SortMe

Func _fini()
    _SQLite_Shutdown()
    Exit
EndFunc   ;==>_fini

kylomas

Apparently I was calling it wrong or something.. Don't know what I was doing wrong, but it's working now. Thanks dude :) 

Link to comment
Share on other sites

A little late I know, only curious to how will this work for you ?

Edit:Edit: actually it looks right to me ;fixed a typo

#include <File.au3>

Local $a[1]
_FileReadToArray(@ScriptDir & "/samplefile.txt", $a, 4, "-")
ReDim $a[UBound($a)][3]
_ArraySort($a, "", "", "", 1)

For $i = UBound($a) - 1 To 0 Step -1
    $a[$i][2] = $a[$i][0] & "-" & $a[$i][1]
    $b = StringRegExp($a[$i][2], "(\D*)(.*)\s([\d|\.]+.*)", 3)
    $b[2] &= $b[1]
    Switch $a[$i][0]
        Case "MB"
            $a[$i][0] = $b[2] & 1
        Case "LCS"
            $a[$i][0] = $b[2] & 2
        Case "LCSD"
            $a[$i][0] = $b[2] & 3
        Case Else
            $a[$i][0] = $b[2]
    EndSwitch
Next

_ArraySort($a, "", "", "", 0)
_ArrayColDelete($a, 1, True)
_ArrayColDelete($a, 0, True)

Local $Array = _ArrayUnique($a)
_ArrayDisplay($Array)

 

Edited by Deye
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...