DALLASCAO.COM

Site of Cao Shouguang, English to Chinese translator

Sort strings by length (autohotkey)

the_arr:=["She", "loves", "me", "him", "Li"]
<ins datetime="2017-10-20T11:00:44+00:00">
the_sorted_arr:=sort_by_length(the_arr)

for key, val in the_sorted_arr
{
    msgbox, % val
}

sort_by_length(the_arr){
    arr_temp:=[]
;set the length of a string as the key for the string and write to a new array
    for key, val in the_arr
    {
        this_len:= strlen(val)
        if (arr_temp[this_len]="") {
            ObjRawSet(arr_temp, this_len, val)         
        } else {
            arr_temp.InsertAt(this_len, val)           
        }
    }
    return arr_temp
}

Leave a Comment