Sort Window

By BlueThen on Apr 18, 2010

Screenshots

This command will let you sort data in windows.

Syntax: /sortWin window token start end
(start and end are optional)
Items in the window will be sorted by the second token
eg. your token is 32 (ascii for space), and your window is
Thomas 16
Ashley 2
Johnny 8

(/sortWin @window 32) would reordered it as
Ashley 2
Johnny 8
Thomas 16

To test it, type use the /sort.test, then use /sortWin on @sW using token 32

This is handy for calculating ranking on a larger scale than what sorttok can handle.

Feel free to do whatever you want with it.

/*
Sort Window
Made by BlueThen on April 18, 2010
www.bluethen.com

Syntax: /winsrt window token start end
Items in the window will be sorted by the second token
eg. your token is 32 (ascii for space), and your window is
Thomas 16
Ashley 2
Johnny 8

(/sortWin @window 32) would reordered it as
Ashley 2
Johnny 8
Thomas 16

To test it, type use the /sort.test, then use /sortWin
*/
alias sortWin {
  %token = $2
  tokenize 32 $1 $iif($3, $3, 1) $iif($4, $4, $line($1, 0))
  %i = $2
  %k = $3
  if ($calc($3 - $2) >= 1) {
    %pivot = $gettok($line($1, $2), 2, %token) 
    while (%k > %i) {
      while (($gettok($line($1, %i), 2, %token) <= %pivot) && (%i <= $3) && (%k > %i)) {
        inc %i 
      }
      while (($gettok($line($1, %k), 2, %token) > %pivot) && (%k >= $2) && (%k >= %i)) {
        dec %k
      }
      if (%k > %i) {
        swap $1 %i %k
      }                      
    }
    swap $1 $2 %k 
    sort.con $1 %token $calc(%k + 1) $3 
    sort.con $1 %token $2 $calc(%k - 1)
  }
}
alias -l swap {
  %T = $line($1, $3)
  rline $1 $3 $line($1, $2)
  rline $1 $2 %T
}
alias -l sort.con {
  sortWin $1-
}
alias sort.Test {
  window -c @sw
  window -e1k0z @sW
  aline @sW Joey 3
  aline @sW Kayla 14
  aline @sW Phillip 19
  aline @sW Sarah 7
  aline @sW Ashley 2
  aline @sW Johnny 8
  aline @sW Bobby 2
  aline @sW Thomas 16
}

Comments

Sign in to comment.
Jethro   -  Apr 19, 2010

Ok looks like it works that way, too. :-)

 Respond  
WorldDMT   -  Apr 19, 2010

@jethro_: no need for 2 "w" try it if u want :p

 Respond  
BlueThen   -  Apr 19, 2010

Thanks for that.

I'm leaving up the code, just in case someone wants to put it into some other use, or convert it to a different language or something.

 Respond  
Jethro   -  Apr 19, 2010

Yes, my bad. It should be 2, not 1. (I edited it)

Oh WorldDMT, you missed a w switch:> -wwtucSince you sort a window numeric values, clear them and then transfer the result to another. :p

 Respond  
WorldDMT   -  Apr 19, 2010

@jethro_: it's 2 not 1 bcz is a filter by age so the second not the first


/filter -wtuc 2 32 @win @win(/code]
 Respond  
BlueThen   -  Apr 18, 2010

...Oh.

 Respond  
Jethro   -  Apr 18, 2010

There is a filter command that you can do sorting without using the while loop:

/filter -wtuc 2 32 @window @window
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.