Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

simple question about the Sort command


  • Please log in to reply
3 replies to this topic
Benny-D
  • Members
  • 1485 posts
  • Last active: Jul 14 2014 02:48 PM
  • Joined: 29 Feb 2008
I wrote this script to sort the contents of my table by the third column (it contains 3-digit numbers) :
list=
(
turtles	cats	567	December	John
crocodiles	dogs	555	January	Michael
iguana	mice	789	May	Jack
snake	elephants	112	April	Pat

)

loop, parse , list, `n,
{
if A_LoopField = 
  break
checkpos:= RegExMatch(A_LoopField, "(.*?)`t(.*?)`t(.*?)`t(.*?)`t(.*)", check)
newlist= %newlist%%check3%`t%check1%`t%check2%`t%check4%`t%check5%`n
}
Sort, newlist
list=

loop, parse , newlist, `n,
{
if A_LoopField = 
  break
checkpos:= RegExMatch(A_LoopField, "(.*?)`t(.*?)`t(.*?)`t(.*?)`t(.*)", check)
list= %list%%check2%`t%check3%`t%check1%`t%check4%`t%check5%`n
}
newlist=
msgbox, %list%
I wonder if there is an easier way to do it in AHK? Perhaps, there is this kind of feature in the Sort command, but I overlooked it.

Benny-D
  • Members
  • 1485 posts
  • Last active: Jul 14 2014 02:48 PM
  • Joined: 29 Feb 2008
Just now I came up with a better script, I guess:
FileRead, corn, *t table.txt

x:= "`n" ; delimiter between rows of the tables
y:= "`t" ; delimiter between columns
z:= 4 ; the column to be sorted
l:= corn ; the name of the string containing the table

Loop, Parse, l, %x%,
{
carpet:= A_LoopField
if carpet=
 break
counter:=1
  Loop, Parse, carpet, %y%,
  {
   if (counter<z)
      {
      in:=counter+1
      c%in%:= A_LoopField
      }
   if (counter=z)
      c1:= A_LoopField
   if (counter>z)
      c%counter%:= A_LoopField
   counter+=1
  }
   counter-=1
   Loop, %counter%
  {
   p:= c%A_Index%
   if A_Index = 1
      {
      line= %line%%p%
      continue
      }            
   line= %line%`t%p% 
  }
   nlist= %nlist%%line%`n
   line=   
}
sort, nlist
fileappend, %nlist%, table_sorted.txt

It puts the column that is to be sorted to the leftmost position relative to other columns. But this script is also quite slow, especially with big tables. In "Access" the same operation runs much faster. So, I think there should be a faster way to do it in AHK. Do you know how?

nick
  • Members
  • 549 posts
  • Last active: Jul 03 2010 09:31 PM
  • Joined: 24 Aug 2005
Maybe you want to try ColumnSort(). For better performance use SetBatchlines, -1.

Benny-D
  • Members
  • 1485 posts
  • Last active: Jul 14 2014 02:48 PM
  • Joined: 29 Feb 2008
Thank You, nick , I think that's something what I was looking for. Now I need some time to sudy that script. Too bad, all the comments there are in German - i can't understand anything.

In the "Help" file under SteBatches title it is written: "Use SetBatchLines -1 to never sleep (i.e. have the script run at maximum speed)." I have a question here - why not just run all the scripts at maximum speed? Why are these sleeps needed at all?