I'm not really sure this addition is filtering, the way I am used to it, and the sort seems computationally expensive (not that it probably matters in the long run).  Why not just use an exponential filter and add the ability to pass an (optional) filter coefficient on the command line?