Jump to content

Sort strings by letter(s)


 Share

Recommended Posts

I have a list of words

$a[0]="bob"

$a[1]="betty"

$a[2]="cindy'

$a[3]="cherry"

How can I perform sorting by letter? for example by letter "b", then I hav as a result only "bob" and "betty" or by letters "be" then I have only "betty". How to do it programmatically?

Link to comment
Share on other sites

  • Moderators

topten,

This old script of mine should give you some ideas - it takes a list of random 4-letter "words" and selects those which match the letters entered into the edit:

#include <GUIConstantsEx.au3>
#include <Array.au3>
#Include <GuiListBox.au3>

Global $hGUI, $hInput, $hList, $sPartialData, $asKeyWords[100]

; Create list full of random 5 character "words"
Keywords()

$hGUI = GUICreate("Example", 200, 400)
$hInput = GUICtrlCreateInput("", 5, 5, 190, 20)
$hList = GUICtrlCreateList("", 5, 30, 190, 325, BitOR(0x00100000, 0x00200000))
$hButton = GUICtrlCreateButton("Read", 60, 360, 80, 30)
$hUP = GUICtrlCreateDummy()
$hDOWN = GUICtrlCreateDummy()
$hENTER = GUICtrlCreateDummy()
GUISetState(@SW_SHOW, $hGUI)

; Set accelerators for Cursor up/down and Enter
Dim $AccelKeys[3][2]=[["{UP}", $hUP], ["{DOWN}", $hDOWN], ["{ENTER}", $hENTER]]
GUISetAccelerators($AccelKeys)

$sCurr_Input = ""
$iCurrIndex = -1

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hList

            $sChosen = GUICtrlRead($hList)
            If $sChosen <> "" Then GUICtrlSetData($hInput, $sChosen)
        Case $hButton

            If $sPartialData <> "" Then
                $sFinal = GUICtrlRead($hInput)
                If _ArraySearch($asKeyWords, $sFinal) > 0 Then
                    MsgBox(0, "Chosen", $sFinal)
                EndIf

            EndIf

        Case $hUP

            If $sPartialData <> "" Then
                $iCurrIndex -= 1
                If $iCurrIndex < 0 Then $iCurrIndex = 0
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf

        Case $hDOWN

            If $sPartialData <> "" Then
                $iTotal = _GUICtrlListBox_GetCount($hList)
                $iCurrIndex += 1
                If $iCurrIndex > $iTotal - 1 Then $iCurrIndex = $iTotal - 1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf

        Case $hENTER

            If $iCurrIndex <> -1 Then
                $sText = _GUICtrlListBox_GetText($hList, $iCurrIndex)
                GUICtrlSetData($hInput, $sText)
                $iCurrIndex = -1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf

    EndSwitch



    ; If input has changed, refill list with matching items
    If GUICtrlRead($hInput) <> $sCurr_Input Then
        CheckInputText()
        $sCurr_Input = GUICtrlRead($hInput)
    EndIf



WEnd



Func CheckInputText()

    $sPartialData = "|" ; Start with delimiter so new data always replaces old
    Local $sInput = GUICtrlRead($hInput)
    If $sInput <> "" Then
        For $i = 0 To 99
            If StringInStr($asKeyWords[$i], $sInput) <> 0 Then $sPartialData &= $asKeyWords[$i] & "|"
        Next
        GUICtrlSetData($hList, $sPartialData)
    EndIf

EndFunc   ;==>CheckInputText



Func Keywords()

    Local $sData

    For $i = 0 To 99
        $asKeyWords[$i] = Chr(Random(65, 90, 1)) & Chr(Random(65, 90, 1)) & Chr(Random(65, 90, 1)) & Chr(Random(65, 90, 1))
        $sData &= $asKeyWords[$i] & "|"
    Next
    GUICtrlSetData($hList, $sData)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)

EndFunc   ;==>Keywords

Let me know if I can help adapt it to meet your specific requirements.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I wrote this simple example.

#include <Array.au3>


Local $a = ["bob", "betty", "cindy", "cherry"]

Local $aNewArray = 0

_ArrayDisplay($a)

$aNewArray = _GetArrayByletter($a, "b")
_ArrayDisplay($aNewArray)
$aNewArray = _GetArrayByletter($a, "ci")
_ArrayDisplay($aNewArray)
$aNewArray = _GetArrayByletter($a, "be")
_ArrayDisplay($aNewArray)

Func _GetArrayByletter($Array, $Letter)
    Local $aReturn[0]
    If IsArray($Array) Then
        For $i = 0 To UBound($Array) - 1
            If StringInStr($Array[$i], $Letter) = 1 Then
                ReDim $aReturn[UBound($aReturn) + 1]
                $aReturn[UBound($aReturn) - 1] = $Array[$i]
            EndIf
        Next
    EndIf
    If UBound($aReturn) = 0 Then $aReturn = 0
    Return $aReturn
EndFunc   ;==>_GetArrayByletter

 

 

Saludos

Link to comment
Share on other sites

bored...

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <array.au3>
#include <string.au3>

#AutoIt3Wrapper_Add_Constants=n

Local $hGui010 = GUICreate('', 230, 250)
GUICtrlCreateLabel('Search Argument:', 10, 20, 110, 20, $SS_CENTERIMAGE)
GUICtrlSetFont(-1, 9, 800)
Local $cInp010 = GUICtrlCreateInput('', 120, 20, 100, 20)
Local $cLst010 = GUICtrlCreateList('', 120, 50, 100, 200)
Local $cWrd010 = GUICtrlCreateLabel('', 10, 52, 100, 184, BitOR($ss_sunken, $ss_center))
GUICtrlSetFont(-1, 9, 800)
GUICtrlSetColor(-1, 0xfafafa)

GUISetState()

Local $sWordList = 'Ann Jack house green turtle coffee Agamemnon eloquent hubris chortle aggregate jamboree'
Local $save_last_input

GUICtrlSetData($cWrd010, stringreplace($sWordList, ' ', @CRLF))

local $atmp



While 1

    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
    EndSwitch



    If $save_last_input <> GUICtrlRead($cInp010) Then
        $save_last_input = GUICtrlRead($cInp010)
        if stringlen($save_last_input) = 0 then
            guictrlsetdata($cLst010,'')
            continueloop

        endif

        $atmp = StringRegExp($sWordList, '(?i)\b' & $save_last_input & '(?:\w+)?\b', 3)
        GUICtrlSetData($cLst010, ( @error ) ? '' : '|' & _ArrayToString($atmp, '|'))
    EndIf

WEnd

 

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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...