Code:
txt=
(Join
13`tC`n
278`tDog`n
13`tB`n
90`tA`n
3`tCobra`n
54`tAlbatross`n
90`tB`n
13`tA`n
511`tC`n
238`tCat`n
90`tC`n
511`tB`n
1`tStoat`n
55`tPanda`n
511`tA`n
)
MsgBox, % WeirdAlphaNumericalizer(txt)
Return
WeirdAlphaNumericalizer(txt)
{
sort, txt, n ;sorts numerically first - the following loop depends on the numbers being grouped together
pos=1
StringLen, lentxt, txt
Loop, % lentxt
{ If(lineEnd := InStr(txt, "`n", 0, pos)) ;get the current line
{ StringMid, currLine, txt, pos, lineEnd-pos
pos := lineEnd+1
If(lineEnd := InStr(txt, "`n", 0, pos)) ;get the next line
{ StringMid, nextLine, txt, pos, lineEnd-pos
RegExMatch(nextLine, "m)^[\d]+?(?=`t)", nextnum) ;get just the next number
}
}
If(currLine)
{ RegExMatch(currLine, "m)^[\d]+?(?=`t)", num) ;get just the number
If(num=prevNum and num!=nextNum) ;if the last one in the group
{ StringSplit, tmpThing, currLine, %A_Tab%
tmpList .= tmpThing2 . "`t" . tmpThing1 . "`n"
If(tmpList) ;if this list exists, it has several switched items.
{ Sort, tmpList ;Sort them alphabetically
newtmpList=
Loop, Parse, tmpList, `n ;put 'em back
{ StringSplit, tmpThing, A_LoopField, %A_Tab%
If(tmpThing0)
{ newtmpList .= tmpThing2 . "`t" . tmpThing1 . "`n"
}
}
newtxt .= newtmpList ;Add sorted list to new list
tmpList= ;clear it
}
}
Else If(num=prevNum and num=nextNum) or (num!=prevNum and num=nextNum) ;if same as next or last one, switch word and number and add to temp list
{ StringSplit, tmpThing, currLine, %A_Tab%
tmpList .= tmpThing2 . "`t" . tmpThing1 . "`n"
}
Else ;if not same as next or prev one, add to final list
{ newtxt .= currLine . "`n" ;just add the curr item
}
prevNum := num
currLine= ;clear
prevLine=
nextNum=
}
}
Return newtxt
}