AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Func] sort by numbers within a string

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
haichen



Joined: 05 Feb 2007
Posts: 140
Location: Osnabrück, Germany

PostPosted: Tue Sep 16, 2008 4:43 pm    Post subject: [Func] sort by numbers within a string Reply with quote

Maybe someone has need for it.

Code:
MyVar1 = asdTest - 12 - Bla`nTest - 13 - Bla`naTest - 4 - Blabla`nTest - 8 - Bla`nTest - 14 - Bla`nTest - 5 - Bla`nTest - 6 - Bla`nTest - 7 - Bla`nTest - 1 - Bla`nTest - 10 - Bla`nTest - 11 - Bla`nTest - 2 - Bla`nTest - 3 - Bla`nTest - 9 - Bla`nTest - 15 - Bla
MyVar = asadaz5.txt,z3.txt,z7.txt,z9.txt,hghjghz1.txt,zsdf13.txt,z999.txt,z-4.txt
MsgBox % sortByNumberWithin(MyVar,",")
MsgBox % sortByNumberWithin(MyVar1,"`n")

sortByNumberWithin(str,del)
{
  x=`````````````````

  loop,parse,str,%del%
  {
    RegExMatch( a_loopfield,"(\D*)(-?\d*)(.*)",nbr )
    nl.=strlen(nbr1) . ","
  }
  Sort nl,R N D, 
  StringSplit, mL,nl,`, 
  ml:=ml1

  astr=
  loop,parse,str,%del%
  {
    RegExMatch( a_loopfield,"([a-zA-Z]+|\D*)-?\d+",nbr ) ;([a-zA-Z]+)-?\d+
    sal:=ml-strlen(nbr1)+1
    sa:=substr(x,1,sal)
    astr.=sa . a_loopfield . ","
  }

  start:=ml+2
  Sort astr,P%start% N D, 
  StringReplace, astr,astr,``,,all
  StringReplace, astr,astr,`,,%del%,all
  StringTrimRight, astr, astr, 1
  return astr
}


Result:
Quote:
---------------------------
sorttest.ahk
---------------------------
asadaz5.txt,z3.txt,z7.txt,z9.txt,hghjghz1.txt,zsdf13.txt,z999.txt,z-4.txt
->
z-4.txt,hghjghz1.txt,z3.txt,asadaz5.txt,z7.txt,z9.txt,zsdf13.txt,z999.txt
---------------------------

asdTest - 12 - Bla
Test - 13 - Bla
aTest - 4 - Blabla
Test - 8 - Bla
Test - 14 - Bla
Test - 5 - Bla
Test - 6 - Bla
Test - 7 - Bla
Test - 1 - Bla
Test - 10 - Bla
Test - 11 - Bla
Test - 2 - Bla
Test - 3 - Bla
Test - 9 - Bla
Test - 15 - Bla
->
Test - 1 - Bla
Test - 2 - Bla
Test - 3 - Bla
aTest - 4 - Blabla
Test - 5 - Bla
Test - 6 - Bla
Test - 7 - Bla
Test - 8 - Bla
Test - 9 - Bla
Test - 10 - Bla
Test - 11 - Bla
asdTest - 12 - Bla
Test - 13 - Bla
Test - 14 - Bla
Test - 15 - Bla
Smile

Last edited by haichen on Wed Sep 17, 2008 1:39 am; edited 2 times in total
Back to top
View user's profile Send private message
haichen



Joined: 05 Feb 2007
Posts: 140
Location: Osnabrück, Germany

PostPosted: Wed Sep 17, 2008 1:37 am    Post subject: Reply with quote

I changed the code for sorting more and different strings.
Back to top
View user's profile Send private message
bradchuck



Joined: 01 Apr 2008
Posts: 4

PostPosted: Wed Sep 17, 2008 3:26 am    Post subject: thanks Reply with quote

I seem to fall back to ahk when I need to do stuff like this.

I can make a quick GUI and throw it into an .exe and email it to whoever needs it.

I don't have a use for it now but it's here if I do.

Thanks for posting it.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group