How many times have you needed to sort a file based on random or arbitrary criteria?  Well, probably not too frequently, and even if you have the need, you should be able to write one in less time than it takes to find a base.  But let’s ignore that.

One solution might be to do some fancy-schmancy mapping operation on the file, such that your data is preserved, but an alphabetical sort will give you your desired order, after which point you can remove whatever processing you did to it.

That sounds like it would be painful.  Or fun, if you have enough time or an interesting idea crosses your mind.

At any rate, a friend of mine recently was faced with an interesting dilemma, where he needed to sort a file based on some unusual criteria.  Here are the basics of his problem.  He forgot the last few characters in a password, but he was fairly sure they were letters and lower-case numbers.  So, he wanted to prioritize the the order in which he checked the 15 million combinations (62^4).

I whipped something together quick and dirty in C++.  It is not the most efficient, but it handled his 300M file in ~4 minutes while using 1 gig of ram.  As I said, it is quick and dirty, although I am especially surprised that it took up that much space in memory, especially considering that it was using std::sort, a modified quicksort.

At any rate, I’ve changed a few things in order to imitate more closely the unix sort’s behavior.  In order to sort, just define whatever criteria you want to in the funky_summer function.

// originally made for a friend so he could sort a file peculiarly
// done on Wed, 30 July, 2008
// by David Hilton, posted at davidhilton.wordpress.com

#include
#include
#include
#include

using namespace std;

vector data;

// The core of the all-important comparator – customize this
int funky_summer(string str) {
int tmp = 0;
for(int i=16; i>buf;

if(buf.length() > 0)
data.push_back(buf);
}
}

void output_sort() {
vector::iterator i;
for( i = data.begin(); i != data.end(); i++ ) {
cout << *i << endl; // to help debug the funky_summer function //cerr << funky_summer(*i) << "\t" << *i << endl; } } void print_help() { cerr<<"The funky sorter has similar behaviour to sort. It takes lines, compares them,"< datatowrite”<