As I understand it the input file will already be sorted in some order that matters to you, with the Row id signifying that importance. You then want to re-sort them on a priority of least UII's while maintaining some bleed through of importance or weight given to the Row id and also applying two additional weighting criteria of '+n' and '-k'. The end result should be such that if you add n and or k (not stated as you will just that you need the option) the greatest weight should be given to UII's next order of importance would be Row id and you want all rows shifted so that they are ranked no lower than Row id-n and no higher than Row id +k. I think it would be possible to place priorities on the criteria but setting them as absolutes could very well hang your server in an infinite loop. on a 4000 row file I can almost guarantee that you would have 2 or more rows that would infinitely swap back and forth as the n then k criteria were applied and re-applied.
That said if filtering the results with one as an absolute and the second as a preference the server would sort by n, apply k, then verify that the final product didn't violate n and correct any rows that did.
last possible solution would simply weight each row based on how well they met each of the three criteria so that the final order would be achieved in a single run and you could set a weight for each column ranging from one to one to ten to one or whatever ratios you felt would be appropriate.
Clearly I could resolve it better if I had more information, but understand your need to be obscure. Just bear in mind if I am awarded the project the more you can disclose the better the chances of my building you the best final product possible.
Thank you for your consideration,
Matthew